VBA Excel – 编译错误对象必需

披露:我在编码方面相当缺乏经验,但是对其背后的逻辑有一个合理的理解,而且经常只是需要一点点力气才能获得语法的权利等等。

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

目标:我想要做的是创build一个电子表格,其中最上面一行是连续date列表。 前几列是账单数据等。我想要我的macros是看看账单的金额,开始和结束date和账单的频率(每周/每月等),然后填充单元格账单到期date栏中的同一行。 我花了最后一天的代码,我很高兴,直到我去运行它。 我已经摆脱了一些错误,我使用了一个variables。值显然不存在,我弄乱了单元格(行,列)的语法。

我现在遇到的问题是编译错误:对象在这一行上必需的:

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

该行应该做的是在第一行中查找“currentDate”的所有date,然后将其存储为dateAddress,以便随后可以在下一行代码中使用dateAddress.Column以及currentRow,find与帐单金额填充的正确单元格。

我清楚了吗? 我的代码如下。

我的代码:

 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)

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


2)

由于声明为String的variables(如Dim dateAddress as String )不是对象,所以不应该使用Set来初始化它。 因此,

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

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

你应该检查这个


3)

在发布的链接逻辑之后,你可以声明一个variables为:

 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 

address属性不是一个对象,所以你不需要使用set

代替

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

使用

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