【发布时间】:2014-02-13 10:17:19
【问题描述】:
披露:我在大多数类型的编码方面相当缺乏经验,但对其背后的逻辑有合理的理解,并且通常只需要一点点推动即可获得正确的语法等。
我要做的是创建一个电子表格,其中顶行是连续日期的列表。前几列是账单等数据。我希望我的宏做的是查看账单金额、开始和结束日期以及账单的频率(每周/每月等),然后填充单元格账单到期日期列中的同一行。我花了最后一天想出这段代码,我对它非常满意,直到我去运行它。我已经摆脱了一些我使用 variable.Value 的错误,这些错误显然不存在,但现在我遇到了一些我无法掌握的问题。而且我没有运气搜索互联网。
调试发现的第一个问题就在这一行:
currentDate = Cells(G, currentRow).Value 'Set Start Date
我收到运行时错误“1004”:应用程序定义或对象定义错误。我敢肯定这只是一个简单的语法问题或其他东西。该行应该做什么(在第一个实例中)是查看单元格“G3”中的开始日期,该日期格式为日期(04-Feb-14)并分配变量 currentDate到那个日期,这样我就可以在第一行的所有日期中搜索那个“currentDate”。
我说得够清楚吗?我的代码如下。
我的代码:
Private Sub CommandButton1_Click()
Dim currentDate As Date
Dim currentRow As Integer
Dim repeatuntilDate As Date
Dim repeatuntilRow 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(G, currentRow).Value 'Set Start Date
repeatuntilDate = Cells("H,currentRow").Value 'Set End Date
While currentDate <= repeatuntilDate 'Loop from Start Date until End Date
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("E,currentRow").Value = "Weekly" Then
currentDate = DateAdd("ww", 1, currentDate)
ElseIf Cells("E,currentRow").Value = "Fortnightly" Then
currentDate = DateAdd("ww", 2, currentDate)
ElseIf Cells("E,currentRow").Value = "Monthly" Then
currentDate = DateAdd("m", 1, currentDate)
ElseIf Cells("E,currentRow").Value = "Quarterly" Then
currentDate = DateAdd("q", 1, currentDatee)
ElseIf Cells("E,currentRow").Value = "6 Monthly" Then
currentDate = DateAdd("m", 6, currentDate)
ElseIf Cells("E,currentRow").Value = "Annually" Then
currentDate = DateAdd("y", 1, currentDate)
' ElseIf Cells("E,currentRow").Value = "Once off" Then
' Exit While
End If
Wend
currentRow = currentRow + 1 'Once row is complete, increment to next row down
Wend
End Sub
【问题讨论】: