VBA Excel-运行时错误';1004';尝试为单元格赋值时

VBA Excel-运行时错误';1004';尝试为单元格赋值时,vba,excel,cells,Vba,Excel,Cells,有关背景信息,请参见。((我之前发布了相同的代码,但有不同的问题。但是,这是我遇到的新问题。) 我已经排除了一些使用变量.Value的错误,它扰乱了单元格(行、列)的语法,我刚刚修复了.Find,以便现在正确使用它 我原以为我已经完成了这段代码,但现在我收到了一个运行时错误“1004”:应用程序定义的或对象定义的错误消息,这一行: Cells(currentRow, dateAddress).Value = billAmount 'Cells(currentRow, "D").Value 'P

有关背景信息,请参见。((我之前发布了相同的代码,但有不同的问题。但是,这是我遇到的新问题。)

我已经排除了一些使用变量.Value的错误,它扰乱了单元格(行、列)的语法,我刚刚修复了
.Find
,以便现在正确使用它

我原以为我已经完成了这段代码,但现在我收到了一个运行时错误“1004”:应用程序定义的或对象定义的错误消息,这一行:

Cells(currentRow, dateAddress).Value = billAmount 'Cells(currentRow, "D").Value 
'Populate cell with amount
我尝试过使用
Set
。我尝试过创建一个变量
billmount
,从
单元格(currentRow,“D”).value中为其赋值,然后将
单元格(currentRow,dateAddress)赋值。如上所述

我试过让一个单元格与另一个单元格相等,但这也不起作用

在调试模式下,如果我将鼠标悬停在
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
Private子命令按钮1\u单击()
将当前日期设置为日期
将currentRow设置为整数
Dim repeatuntilDate作为日期
Dim repeatuntilRow为整数
Dim dateAddress作为字符串
日期范围变暗为范围
将最后日期设置为范围
将billAmount设置为整数
currentRow=3'条目的第一行
repeatuntilRow=单元格。查找(“*”,搜索顺序:=xlByRows,搜索方向:=xlPrevious)。行“最后一行条目”
而currentRow
Cells(currentRow, dateAddress).Value = billAmount
dateAddress
变量声明为整数,因为它包含整数和
单元格(currentRow,dateAddresse)
以及
dateAddress
作为
String
包含数字将抛出
运行时错误1004

Dim dateAddress as Integer

最后一件事,我建议您将
dateAddress
更改为
dateCol
或其他一些东西,这样在编写代码后重新检查代码时,对其他人甚至对您来说都更清晰。

实际上,我在那里有点鬼鬼祟祟,您可能没有看到。上面的一行内容是
dateAddress=dateRange.Column
,因此在调用我时在
单元格()
中的t实际上已经是列的当前值。恐怕还有其他可疑之处。@Bzmek;对不起,我的不好。记得昨天的代码;)但是,由于
日期地址
现在包含整数(因为
返回一个克隆数),您应该这样声明它(
Dim dateAddress as Integer
)然后试试。啊,你是一个传说中的简单人!这就解决了问题。是的,我想你可能还在想昨天的代码:)现在解决了,效果很好。看起来我还有最后一个错误,
currentDate
在开始日期在我的范围内第一个可能的日期之前时失控。我想我只需要做一个检查循环,以确保这是处理好了,一切都应该是好的。非常感谢。没有必要在每篇文章中都发表同样的500字免责声明和对同一件事的冗长解释。您可以链接到初始问题以参考背景,并完全排除重复的免责声明。过于罗嗦的帖子几乎和信息太少的帖子一样糟糕——当你在所有不必要的噪音和混乱中挣扎时,你很难理解所问的问题。我已经清理了其中的一些内容,试图让它更容易理解。:-)收到。下次我一定记住这一点。为最后两个冗长的问题道歉。