【发布时间】:2014-03-11 19:07:16
【问题描述】:
披露:我在大多数类型的编码方面相当缺乏经验,但对其背后的逻辑有合理的理解,并且通常只需要一点点推动即可获得正确的语法等。
我之前发布了相同的代码,但有一个不同的问题,并且没有发现这个问题,所以我认为最好为它创建一个新问题
目标:我要做的是创建一个电子表格,其中顶行是连续日期的列表。前几列是账单等数据。我希望我的宏做的是查看账单金额、开始和结束日期以及账单的频率(每周/每月等),然后填充单元格账单到期日期列中的同一行。我花了最后一天想出这段代码,我对它非常满意,直到我去运行它。我已经摆脱了一些我使用变量的错误。显然不存在的值并且我弄乱了 Cells(row, column) 的语法。
我现在遇到的问题是编译错误:此行需要对象:
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
【问题讨论】: