【发布时间】:2014-02-14 09:19:11
【问题描述】:
有关背景,请参阅my previous question。 (((我之前贴过同样的代码,但是有不同的问题。不过,这是我遇到的一个新问题。)
我已经摆脱了一些我使用 variable.Value 的错误,弄乱了 Cells(row, column) 的语法,我刚刚修复了 .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
【问题讨论】:
-
没有必要在每篇文章中都发布相同的 500 字免责声明和对同一事物的冗长解释。您可以链接到最初的问题以参考背景,并完全排除重复的免责声明。过于冗长的帖子几乎与包含太少信息的帖子一样糟糕——当您涉足所有不必要的噪音和混乱时,很难弄清楚被问到的问题。我已经清理了其中的一些内容,以便更容易理解。 :-)
-
罗杰。下次我一定会记住这一点。对于最后两个冗长的问题,我们深表歉意。