VBA:函数给出“运行时错误'424':所需的对象”调用时出错

我有两个主要function,第一个是search_bank。 它会逐个单元格地searchCredits,Type和Store列,并判断是否匹配。如果匹配,则返回True,并且副作用会改变匹配单元格的颜色。

我用来testing第一个函数的第二个子 。 我遇到的问题是我得到运行时错误'424':所需的对象 ,没有迹象表明问题是在哪里。

这是第一个function:

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 

这里是testing者:

 Sub Tester() Dim x As Boolean x = search_bank("ctc", 38.4, True) Debug.Print (x) End Sub 

我曾尝试在testing仪上使用'set':

 Sub Tester() Dim x As Boolean Set x = search_bank("ctc", 38.4, True) Debug.Print (x) End Sub 

甚至在将variables传递给testing人员之前声明variables(我不是很习惯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 

在你的OP下有很多好评,还有@ BrandonBarney的回答,但是这是我的两分钱:

分一:我看到的最大的事情是你从来没有申报blank_sheet但尝试使用它,而设置范围对象。 这是你的错误来自哪里。 它正在寻找Range("1:1").Find("M_STORE") ,但不知道什么bank_sheet是。

分二:有一个快速的方法来指出你是总是使用Option Explicit在代码的顶部。 这可以确保您使用的任何variables都被明确声明。 即:

 Option Explicit 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 ''''' New code here: '''''' Dim bank_sheet as Worksheet Set bank_sheet = Worksheets("Bank Sheet") ' change to whatever the name is. ''''''''''''''''''''''''''' 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") ' etc. etc. 

如果你意外地有一个错字, Option Explicit也会有所帮助。 所以,如果你曾经做过bank_sheeet.Range("A:A")那么就会出错,并要求你申报bank_sheeet 。 或者,当然,你会意识到这是一个错字,然后修复它。

奖金分:你可以结合你的Dim s节省几行:
Dim m_store as Range, m_type as Range, Credit_Amt_Col as Range都可以在一行上。

(注意: Dim m_store, m_type, Credit_Amt_Col as Range 不会将全部三个设置为Rangetypes,它将使m_storem_type成为Variant因为它没有被声明,只有Credit_Amt_Col在这种情况下Credit_Amt_Col Range 。明确说明每个variables的types)。

如果可以的话,我会将其作为评论发表,但我不能。 所以我知道这不会直接解决它,但它将有助于debugging。 见下文:

 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 ' It is always best to check the inverse of an object before setting ' setting an object variable to the target object. In this case ' I check to make sure each range can be found, and if not, I ' debug.print which variable cannot be set. 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") If m_store is Nothing then Debug.Print "m_store is nothing" If m_type is Nothing then Debug.Print "m_type is nothing" If Credit_Amt_Col is Nothing then Debug.Print "Credit_Amt_Col is nothing." 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 ' Use the inverse method above on these three items as well. 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 

我发表了一个内联的评论,但基本上我为你的前三个对象添加了一个反向检查(你也想为你的第二组对象做这个)。 这是最好的做法,但在这种情况下,它也会(希望)帮助您查明对象不能find的位置。