2014-02-13 39 views
2

披露:我在最不爽的編碼相當缺乏經驗,但有其背後的邏輯有一定的瞭解,並經常只需要輕輕一推與得到語法的權利等VBA的Excel - 編譯錯誤所需的對象

我張貼相同的代碼前面,但使用不同的問題,並因此我認爲最好的就是創建一個新的問題,因爲它

目的沒有發現這個問題:我想要做的就是創建一個電子表格,其中第一行是連續da的列表工商業污水附加費。前幾列是賬單數據等。我希望我的宏做的是查看賬單數量,開始和結束日期以及賬單的頻率(每週/每月等),然後填入單元格賬單到期日期欄中的同一行。我花了最後一天來看這段代碼,直到我去運行它,我都非常開心。我已經擺脫了一些使用variable.Value的錯誤,這些錯誤顯然不存在,我搞亂了Cells(行,列)的語法。

說我來面對現在的問題是編譯錯誤:所需的對象在這條線:

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

那是什麼線應該做的是在所有日期的第一行中搜索'currentDate',然後將其存儲爲dateAddress,以便我可以在下一行代碼中使用dateAddress.Column以及currentRow,以找到要用帳單金額填充的正確單元格。

我是否清楚了?我的代碼如下。

我的代碼:

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」)因此您應該將dateAddress聲明爲String而不是Date


2)

由於)聲明爲String(如在Dim dateAddress as String一個變量不是一個對象,則不應使用Set進行初始化。因此,

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

成爲

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

您應該檢查THIS


3)

繼發佈鏈接的邏輯,你可以聲明一個變量:

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人。事實證明,我做錯了一些事情。我的最終代碼現在是: '帶範圍(「J1:AAI1」) 設置lastDate = .Cells(.Cells.Count)'設置爲即將到來的查找開始於lastDate 結束與 設置dateRange = Range(「J1:AAI1」)。Find(What:= currentDate,After:= lastDate)' – Bzmek

0

address屬性不是一個對象,所以你不需要使用set。取而代之的

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

使用

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

感謝您的答覆弗洛里斯。我原本沒有在那裏的'設置',但我得到一個運行時錯誤'91':對象變量或塊變量未設置消息。谷歌搜索這個錯誤是什麼導致我把'集'。這有助於使問題更清楚嗎? – Bzmek

+0

如果你的'Find'沒有返回任何東西,你將無法獲得地址(未找到的地址範圍)。更好的分裂在兩個 - 做的發現,確認它不'沒有',然後得到地址等。 – Floris