VBA Excel - Compile Error Object Required

2020-07-24 04:50发布

问题:

Disclosure: I'm fairly inexperienced at coding of most sorts but have a reasonable understanding of the logic behind it and quite often just need a little push with getting syntax's right etc.

I posted the same code earlier but with a different problem and have no discovered this issue so I thought it best to create a new question for it

Objective: What I'm trying to do is create a spreadsheet where across the top row is a list of consecutive dates. In the first few columns is data for bills etc. What I want my macro to do is look at the amount of a bill, the start and end dates and the frequency of the bill (weekly/monthly etc) and then populate the cells in that same row in the columns of the dates where the bill is due. I've spent the last day coming up with this code and I was pretty happy with it until I went to run it. I've already got rid of a few bugs where I was using a variable.Value which apparently doesn't exist and I had messed up the syntax for Cells(row, column).

The problem that I'm coming up against now is Compile Error: Object Required on this line:

Set dateAddress = Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address 
'find the current date within the range of dates in row 1

What that line is supposed to be doing is searching across all the dates in the first row for that 'currentDate' and then storing that as dateAddress so that I can then use the dateAddress.Column in the next line of code along with the currentRow, to find the right cell that is to be populated with the bill amount.

Am I being clear enough? My code is below.

My Code:

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

回答1:

1)

Range("J1:AAI1").Find(currentDate, LookIn:=xlValues).Address will return an Address in the form of a string (ex.: "$A$1") therefore you should declare dateAddress as a String instead of Date.


2)

Since a variable declared as a String (as in Dim dateAddress as String) is not an object, you should not use Set to initialize it. Hence,

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

becomes

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

You should check THIS.


3)

Following the posted link logic, you could have declared a variable as:

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


回答2:

The address property is not an object, so you don't need to use set.

Instead of

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

Use

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