2014-02-13 3 views
2

공개를 필수 컴파일 나는 대부분의 종류의 코딩에서 상당히 경험이 있어요하지만 그 뒤에 논리의 합리적인 이해하고 자주 단지 구문의 권리 등VBA 엑셀은 - 오류 개체

을 받고있는 약간의 압력이 필요합니다

나는 이전하지만 다른 문제에 동일한 코드를 게시하지 않고 나는 그것이

목적을 위해 가장 새로운 질문을 만들 생각 때문에 더는이 문제를 발견했습니다 :가 난 할 노력하고있어 것은 만드는 것입니다 상단 행을 가로 지르는 연속적인 다 (da)의 목록 인 스프레드 시트 테. 처음 몇 줄에는 청구서 등의 데이터가 있습니다. 제가 수행하기를 원하는 매크로는 청구서 금액, 청구서의 시작과 종료 날짜 및 청구서 수 (주별/월별 등)를 살펴본 다음에 셀을 채 웁니다. 계산서가 만기가되는 날짜의 란에있는 같은 행. 나는이 코드를 다룰 마지막 날을 보냈고 나는 그것을 실행하기까지는 꽤 행복했다. 나는 이미 변수를 사용하고있는 몇 가지 버그를 없앴습니다. 분명히 존재하지 않는 값과 셀 (행, 열)에 대한 구문을 엉망으로 만들었습니다.

지금에 올라오고있어 문제는 컴파일 오류입니다 :이 라인에 필요한 개체 :

Set dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 
'find the current date within the range of dates in row 1 

그 라인의 첫 번째 행에있는 모든 기간에 걸쳐 검색하는 일을 할 예정이다 무엇 그 다음 'currentDate'를 저장 한 다음 dateAddress로 저장하여 다음 코드 줄에서 currentRow와 함께 dateAddress.Column을 사용하여 청구 금액으로 채울 오른쪽 셀을 찾습니다.

충분히 명확합니까? 내 코드는 아래와 같습니다.

내 코드 : 당신이 set를 사용할 필요가 없습니다

Private Sub CommandButton1_Click() 
Dim currentDate As Date 
Dim currentRow As Integer 
Dim repeatuntilDate As Date 
Dim repeatuntilRow As Integer 
Dim dateAddress As Date 
currentRow = 3 'First row of entries 
repeatuntilRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'Last row of entries 
While currentRow < repeatuntilRow 'Loop from first row until last row of entries 
currentDate = Cells(currentRow, "G").Value 'Set Start Date 
repeatuntilDate = Cells(currentRow, "H").Value 'Set End Date 
    While currentDate <= repeatuntilDate 'Loop from Start Date until End Date 
     Set dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 'find the current date within the range of dates in row 1 
     Cells("dateAddress.Column,currentRow").Value = Cells("D,currentRow").Value 'Populate cell with amount 
     'Increment the currentDate by the chosen frequency 
     If Cells(currentRow, "E").Value = "Weekly" Then 
      currentDate = DateAdd("ww", 1, currentDate) 
     ElseIf Cells(currentRow, "E").Value = "Fortnightly" Then 
      currentDate = DateAdd("ww", 2, currentDate) 
     ElseIf Cells(currentRow, "E").Value = "Monthly" Then 
      currentDate = DateAdd("m", 1, currentDate) 
     ElseIf Cells(currentRow, "E").Value = "Quarterly" Then 
      currentDate = DateAdd("q", 1, currentDatee) 
     ElseIf Cells(currentRow, "E").Value = "6 Monthly" Then 
      currentDate = DateAdd("m", 6, currentDate) 
     ElseIf Cells(currentRow, "E").Value = "Annually" Then 
      currentDate = DateAdd("y", 1, currentDate) 
     ' ElseIf Cells(currentRow,"E").Value = "Once off" Then 
      ' Exit While 
     End If 
    Wend 
    currentRow = currentRow + 1 'Once row is complete, increment to next row down 
Wend 
End Sub 

답변

3

1)

Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 문자열의 형태로 Address에게 (예를 반환합니다 : "$ A $ 1") 그러므로 dateAddressDate 대신 String으로 선언해야합니다. Dim dateAddress as String에서와 같이 String (로 선언 된 변수) 객체가 아니기 때문에


2)

, 당신은 그것을 초기화 Set을 사용할 수 없습니다.따라서,

Set dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 

당신은 THIS을 확인해야

dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 

된다. 응답 플로리스에 대한

Dim dateRange as Range 'Range is an object 
'And then Set your Range like: 
Set dateRange = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues) 'Because "Find" returns a range object 

'And then with your dateAddress: 
Dim dateAddress as String 
dateAddress = dateRange.Address 
+0

감사합니다. simpLE MAn. 내가 잘못하고있는 것이 몇 가지 있었다. (작동하는) 내 최종 코드는 다음과 같습니다. '범위 포함 ("J1 : AAI1") lastDate = .Cells (.Cells.Count) '설정 lastDate에서 시작될 다음 찾기 설정 끝내기 집합 dateRange = Range ("J1 : AAI1"). Find (What : = currentDate, After : = lastDate)' – Bzmek

0

address 속성, 객체가 아닙니다.

대신

Set dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 

의 사용은

dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 
+0

감사 : 게시 된 링크의 논리에 따라


3)

, 당신은 변수를 선언 할 수 있었다. 나는 원래 거기에'Set'을 가지고 있지 않았지만 Run-time Error '91'을 얻었습니다 : Object Variable 또는 With 블록 변수가 메시지를 설정하지 않았습니다. 그 오류를 인터넷으로 검색하면 '세트'를 넣을 수 있습니다. 그게 문제를 명확하게하는 데 도움이됩니까? – Bzmek

+0

'Find '가 아무것도 반환하지 않으면 (찾을 수없는 범위의) 주소를 얻을 수 없습니다. 2 개로 분할하는 것이 더 좋습니다. - 찾기를 수행하고, 아무것도 아닌지 확인한 다음 주소 등을 가져옵니다. – Floris