披露:我在最不爽的編碼相當缺乏經驗,但有其背後的邏輯有一定的瞭解,並經常只需要輕輕一推與得到語法的權利等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
謝謝simpLE人。事實證明,我做錯了一些事情。我的最終代碼現在是: '帶範圍(「J1:AAI1」) 設置lastDate = .Cells(.Cells.Count)'設置爲即將到來的查找開始於lastDate 結束與 設置dateRange = Range(「J1:AAI1」)。Find(What:= currentDate,After:= lastDate)' – Bzmek