有关背景信息,请参阅my previous question。 ((我之前发布了相同的代码,但遇到了不同的问题。但是,这是我遇到的一个新问题。)
我已经摆脱了一些我使用变量的错误。值,搞砸了Cells(行,列)的语法,我刚刚修复了.Find
以便我'我现在正确使用它。
我原以为我只是在那里完成了这段代码但是现在我收到了运行时错误'1004':此行上的应用程序定义或对象定义的错误消息:
Cells(currentRow, dateAddress).Value = billAmount 'Cells(currentRow, "D").Value
'Populate cell with amount
我尝试过使用Set
。我尝试创建变量billAmount
,为其分配Cells(currentRow, "D").Value
的值,然后如上所述为其分配Cells(currentRow, dateAddress).Value
。
我试过让一个单元格等于另一个单元格Cells(currentRow, dateAddress).Value = Cells(currentRow, "D").Value
,但这也不起作用。
当处于调试模式时,如果我将鼠标悬停在currentRow
,billAmount
和dateAddress
上,则它们都具有预期值。但是我似乎无法让最后的单元格填充。一旦我解决了这个问题,我确信我必须将它应用到我代码最后部分的所有IF THEN
语句中。但希望它应该是完整的。
这是我的代码:
Private Sub CommandButton1_Click()
Dim currentDate As Date
Dim currentRow As Integer
Dim repeatuntilDate As Date
Dim repeatuntilRow As Integer
Dim dateAddress As String
Dim dateRange As Range
Dim lastDate As Range
Dim billAmount As Integer
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
billAmount = Cells(currentRow, "D").Value
While currentDate <= repeatuntilDate 'Loop from Start Date until End Date
With Range("J1:AAI1")
Set lastDate = .Cells(.Cells.Count) 'Setup for the upcoming Find to begin at the lastDate
End With
Set dateRange = Range("J1:AAI1").Find(What:=currentDate, After:=lastDate)
dateAddress = dateRange.Column 'Obtain column of the found Date
Cells(currentRow, dateAddress).Value = billAmount 'Cells(currentRow, "D").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
答案 0 :(得分:1)
此前:
Cells(currentRow, dateAddress).Value = billAmount
将您的dateAddress
变量声明为整数,因为它包含一个整数,Cells(currentRow, dateAddresse)
包含dateAddress
,String
包含一个数字将Run-time Error 1004
:
Dim dateAddress as Integer
最后,我建议您将dateAddress
更改为dateCol
或其他内容,以便在您编写代码后重新检查代码时,对其他人甚至是您更清楚。