【问题标题】:VBA Excel - Run-time Error '1004' when trying to assign a value to a CellVBA Excel - 尝试为单元格分配值时出现运行时错误“1004”
【发布时间】: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 字免责声明和对同一事物的冗长解释。您可以链接到最初的问题以参考背景,并完全排除重复的免责声明。过于冗长的帖子几乎与包含太少信息的帖子一样糟糕——当您涉足所有不必要的噪音和混乱时,很难弄清楚被问到的问题。我已经清理了其中的一些内容,以便更容易理解。 :-)
  • 罗杰。下次我一定会记住这一点。对于最后两个冗长的问题,我们深表歉意。

标签: vba excel cells


【解决方案1】:

在此之前:

Cells(currentRow, dateAddress).Value = billAmount

将您的dateAddress 变量声明为整数,因为它包含一个整数,而Cells(currentRow, dateAddresse) 与dateAddress 为String 包含一个数字将抛出Run-time Error 1004:

Dim dateAddress as Integer

最后一件事,我建议您将dateAddress 更改为dateCol 或其他内容,这样即使您在编写代码一段时间后重新检查代码,也会对其他人甚至您更清楚。

【讨论】:

  • 实际上我在那儿有点偷偷摸摸,你可能没有看到。上面的行读取dateAddress = dateRange.Column,所以当在Cells() 中调用它时,它实际上已经是列的当前值。恐怕还有其他可疑的事情发生。
  • @Bzmek;对不起这是我的错。记得你昨天的代码 ;) 但是,因为 dateAddress 现在包含一个整数(因为 .Column 返回一个 clolumn 数字)你应该这样声明它(Dim dateAddress as Integer)然后试试看。
  • 啊,你是一个传奇的简单男人!那解决了它。是的,你可能还在想昨天的代码 :) 现在修复了它,它工作得很好。看起来我有一个最终错误,即当开始日期早于我范围内的第一个可能日期时,currentDate 会失控。我想我会做一个检查循环来确保这个问题得到处理,一切都应该没问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多