Excel VBA函数中的错误:“Object required”

在为excel编写函数时出现错误。 该函数的目的是查看表的整个date列,并且对于该列中的每个匹配,从总值中减去该行的成本值(在相邻列中)。 我已经在VBA中写了一个函数,但我甚至不能开始debugging它,因为我被困在第一个错误。 该错误说“对象需要”,并突出显示的第一行,但似乎我已经传递对象(范围)的function。 这是我的代码。

Public Function WeeklyAllowance(firstCell As Range, lastCell As Range) Dim funds As Double Dim today As Date Dim thisWeekday As Integer Dim earlierDay As Date Set funds = 20 today = Now() thisWeekday = Weekday(today) earlierDay = today - thisWeekday + 1 'This should be sunday at first Do Until earlierDay = today + 1 'Basically has one iteration for each day of the week until today Dim cell, column As Range Set column = Range(firstCell, lastCell) For Each cell In column 'Iterate through the cells in the column and check the dates If (cell.Value = earlierDay) Then Dim cellCol, cellRow As Integer cellCol = cell.column cellRow = cell.Row funds = funds - Sheet1.Cells(cellRow, cellCol + 1) End If Next cell earlierDay = earlierDay + 1 'Increment the day to check Loop WeeklyAllowance = funds End Function 

有谁能帮我这个吗?

Set funds = 20是用于对象的语法。 但是你已经将上面定义的funds定义为double (这不是VBA的对象types)。 只要删除Set关键字:

funds=20

 Dim cell, column As Range 

错误。

 Dim cell As Range, column As Range 

如果你循环了一周的几天,而不是加1,那么在工作日使用dateadd函数和参数。