只需点击一下按钮即可运行此宏。我收到错误。
运行时错误'91': 对象变量或未设置块变量
我单击Debug,它会引导我进入这个突出显示的区域。
Selection.Find(What:=DateString, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
这是整个功能
Function GetBalance(Month As Integer) As Currency
'This function is called by the Calculate_Balance subroutine. It
'finds the appropriate month's balance on an employee sheet and sends
'it back to the calling routine.
Dim DateString As String
Dim RangeString As String
Dim Balance
Dim BalDate As Date
Dim strCurrMonth As String
Dim strCurrYear As String
Dim strFirstDayCurrMonth As String
strCurrMonth = CStr(DatePart("m", Date))
strCurrYear = CStr(DatePart("yyyy", Date))
strFirstDayCurrMonth = strCurrMonth & "/1/" & strCurrYear
dtmFirstDayCurrMonth = CDate(strFirstDayCurrMonth)
DateString = Month & "/1/"
Columns("A:A").Select
Range("A6").Activate
Selection.Find(What:=DateString, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
CurrRow = ActiveCell.Row
BalanceRow = CurrRow - 1 'Move up 1 row to get last balance for this month
RangeStr = "E" & BalanceRow
DateRangeStr = "A" & BalanceRow
BalDate = Range(DateRangeStr).Value
If BalDate <= dtmFirstDayCurrMonth Then
Balance = Range(RangeStr).Value
Else
Balance = 0
End If
GetBalance = Balance
End Function
答案 0 :(得分:4)
Find()函数返回一个Range对象,因此如果找不到任何内容,您将收到错误,因为它无法激活&#39;没有。将代码更改为:
Dim rng As Range
Set rng = Selection.Find(What:=DateString, After:=ActiveCell, LookIn:=xlFormulas, SearchDirection:=xlNext, MatchCase:=False)
If Not (rng Is Nothing) Then
rng.Activate
End If