【问题标题】:VBA Excel - Compile Error Object RequiredVBA Excel - 需要编译错误对象
【发布时间】: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

【问题讨论】:

    标签: vba date excel find


    【解决方案1】:

    address 属性不是对象,因此您不需要使用set

    而不是

    Set dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address
    

    使用

    dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address
    

    【讨论】:

    • 感谢弗洛里斯的回复。我最初没有Set,但我收到了运行时错误'91':对象变量或块变量未设置消息。谷歌搜索该错误是导致我输入Set 的原因。这是否有助于使问题更清楚?
    • 如果您的Find 没有返回任何内容,则您无法获取地址(未找到的范围)。最好一分为二 - 查找,确认不是Nothing,然后获取地址等。
    【解决方案2】:

    1)

    Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 将以字符串的形式返回Address(例如:“$A$1”),因此您应该将dateAddress 声明为String 而不是Date


    2)

    由于声明为String(如Dim dateAddress as String)的变量不是对象,因此不应使用Set 对其进行初始化。因此,

    Set dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address
    

    变成

    dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address
    

    你应该检查THIS


    3)

    按照发布的链接逻辑,您可以将变量声明为:

    Dim dateRange as Range 'Range is an object
    'And then Set your Range like:
    Set dateRange = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues) 'Because "Find" returns a range object
    
    'And then with your dateAddress:
    Dim dateAddress as String
    dateAddress = dateRange.Address
    

    【讨论】:

    • 感谢简单的人。事实证明,我做错了几件事。我的最终代码(有效)现在是: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)
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多