我有兩個主要功能,第一個是search_bank。它會逐個單元格搜索Credits,Type和Store列,並判斷是否匹配。如果匹配,則返回True,並且副作用會更改匹配單元格的顏色。VBA:函數給出「運行時錯誤'424':所需的對象」錯誤時調用
第二個sub我用來測試第一個函數。 我遇到的問題是我得到一個運行時錯誤'424':對象需要,沒有指出問題出在哪裏。
這是第一個功能:
Function search_bank(Store As String, amount As Double, Amex As Boolean) As Boolean
Dim m_store As Range
Dim m_type As Range
Dim Credit_Amt_Col As Range
Set m_store = bank_sheet.Range("1:1").Find("M_STORE")
Set m_type = bank_sheet.Range("1:1").Find("M_TYPE")
Set Credit_Amt_Col = bank_sheet.Range("1:1").Find("Credit Amt")
search_bank = False
Dim i As Long
For i = 1 To 9000
If Not search_bank Then
Dim store_cell As Range
Dim type_cell As Range
Dim credit_cell As Range
Set store_cell = Worksheets(2).Cells(i, m_store.Column)
Set type_cell = Worksheets(2).Cells(i, m_type.Column)
Set credit_cell = Worksheets(2).Cells(i, Credit_Amt_Col.Column)
If InStr(UCase(store_cell.Value), UCase(Store)) > 0 And credit_cell.Value = amount Then
If store_cell.Interior.ColorIndex <> 46 Then
If Amex And InStr(UCase(type_cell.Value), UCase("amex deposit")) Then
store_cell.Interior.ColorIndex = 46
search_bank = True
End If
If Not Amex And InStr(UCase(type_cell.Value), UCase("Credit Card Deposit")) Then
store_cell.Interior.ColorIndex = 46
search_bank = True
End If
End If
End If
End If
Next i
End Function
,這裏是測試儀:
Sub Tester()
Dim x As Boolean
x = search_bank("ctc", 38.4, True)
Debug.Print (x)
End Sub
我一直在使用 '設置' 在測試儀上的嘗試:
Sub Tester()
Dim x As Boolean
Set x = search_bank("ctc", 38.4, True)
Debug.Print (x)
End Sub
即使在將它們傳遞給測試人員之前聲明變量(我不太習慣VBA,但一時之間,我相信這只是如此古老,在他們通過之前宣佈的事情)
Sub Tester()
Dim x As Boolean
Dim store As String
Dim Amount As Double
Dim amex As Boolean
store = "ctc"
Amount = 38.4
amex = True
x = search_bank(store, Amount, amex)
Debug.Print (x)
End Sub
當您遇到運行時錯誤時,請選擇調試選項並使用F8逐句通過代碼,直到看到哪行出現錯誤。 –
你在哪裏/何時/如何聲明'bank_sheet'? – BruceWayne
另外,請確保您的三個Find方法的結果返回有效的對象。如果在第1行沒有找到這些值,那麼他們將返回一個'Nothing',這會在代碼中稍後引發此錯誤。 –