我有两个主要功能,第一个是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',这会在代码中稍后引发此错误。 –