VBA Excel - 编译错误对象必需

时间:2014-02-13 04:20:36

标签: excel vba date excel-vba find

披露:我对大多数种类的编码缺乏经验,但对其背后的逻辑有一个合理的理解,而且通常只需要一点点推动语法正确等等。

我之前发布了相同的代码,但遇到了不同的问题但没有发现此问题,所以我认为最好为它创建一个新问题

目标:我要做的是创建一个电子表格,其中顶行是连续日期列表。在前几列是账单等数据。我希望我的宏做的是查看账单金额,开始和结束日期以及账单的频率(每周/每月等),然后填写单元格账单到期日期列中的同一行。我花了最后一天提出这段代码,直到我去运行它,我才非常满意。我已经摆脱了一些错误,我使用的是一个变量.Value显然不存在,而且我搞砸了Cells(行,列)的语法。

我现在遇到的问题是编译错误:此行需要对象:

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

2 个答案:

答案 0 :(得分:3)

<强> 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

答案 1 :(得分:0)

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

而不是

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

使用

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