我有两个主要功能,第一个是search_bank。它逐个单元地搜索Credits,Type和store列,并确定我们是否匹配。如果匹配,则返回True,并且副作用会更改匹配单元格的颜色。
第二个 sub 我用来测试第一个功能。 我遇到的问题是我收到了运行时错误'''对象,但没有说明问题出在何处。
这是第一个功能:
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
我尝试过使用&#39;设置&#39;在测试者身上:
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
答案 0 :(得分:1)
如果可以的话,我会将此作为评论发布,但我不能。所以我知道这不会直接解决它,但它有助于调试。见下文:
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
我发布了内联评论,但基本上我为前三个对象添加了一个反向检查(你也希望为你的第二组对象做这个)。这是最佳做法,但在这种情况下,它(也希望)可以帮助您找到无法找到对象的位置。
答案 1 :(得分:0)
你的OP下有很多好的评论,@ BrandonBarney的答案也是如此,但这是我的两分钱:
第一分钟:我看到的最重要的事情是你从未声明blank_sheet
但是在设置范围对象时尝试使用它。这是您的错误来自的地方。它正在寻找Range("1:1").Find("M_STORE")
,但不知道bank_sheet
是什么。
第二分钟:向您指出的一个快速方法是在代码顶部always use Option Explicit
。这可确保显式声明您使用的任何变量。即:
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
来节省几行:
Dim m_store as Range, m_type as Range, Credit_Amt_Col as Range
都可以在一行上。
(注意:执行Dim m_store, m_type, Credit_Amt_Col as Range
将不将所有三个设置为Range
类型。它会使m_store
和m_type
成为Variant
因为它没有被声明。在这种情况下只有Credit_Amt_Col
是Range
。所以你仍然必须明确说明每个变量的类型。)