【问题标题】:VBA Excel - Problems with a simple macro to auto-fill cells for a budgeting spreadsheet I'm attempting to makeVBA Excel - 我正在尝试制作的预算电子表格的简单宏自动填充单元格的问题
【发布时间】: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

【问题讨论】:

    标签: excel vba date


    【解决方案1】:

    回答为什么你得到Object Defined Error 你错过了Cells 属性语法,即:

    Cells(row_index, col_index) 接受 long 数据类型(数字)。

    应该是这样的:

    currentDate = Cells(currentRow, 7).Value 'G is the 7th column
    

    但类似地,您可以像这样引用col_index 参数:

    currentDate = Cells(currentRow, "G").Value 'take note of the qoutation marks ""
    

    【讨论】:

    • 啊,非常感谢!我应该能够自己挑选那个。这让我可以进一步了解代码并修复一些问题。我在 dateAddress 行中添加了 Set,但现在出现编译错误:此行需要对象:Set dateAddress = Range("J1:AAI1").Find(currentDate).Address 'find the current date within the range of dates in row 1 有什么想法吗?
    • 进一步阐明该特定行。如果我在开始时不包含Set,我会收到运行时错误“91”:未设置对象变量或块变量。
    • HERE 是如何正确使用.Find 属性。仅当您想将 Object 分配给变量时才使用 Set(例如,将范围对象传递给变量的 Set rng = Range("A1"))。范围.Address 属性返回Value(它是字符串格式的范围地址)而不是Object。删除 Set 会导致运行时错误,因为您错过了 .Find 属性的正确语法以及执行它的正确方法。
    • 一旦你阅读了我提供的链接中的文章,尝试适应它,包括正确的循环方式。如果您被困在某个地方,请在 SO 中提出另一个问题。这样其他 VBA 大师在这里可以为您提供帮助,因为我并不总是在附近。 :)
    • 酷!十分感谢!你帮了大忙:)我会看看我读完那篇文章后的表现。
    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 2011-11-21
    • 2015-09-09
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多