I have two main functions, the first is search_bank. It searches through the Credits, Type, and store columns, cell by cell and figures out if we have a match or not.If there is a match, it returns True and as a side effect changes the color of the matched cell.
The second sub I'm using to test the first function. The problem I'm having is that I'm getting a Run time error '424': Object required with no indication of where the issue is.
Here is the first 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
and here is the tester:
Sub Tester()
Dim x As Boolean
x = search_bank("ctc", 38.4, True)
Debug.Print (x)
End Sub
I have tried using 'set' on the tester:
Sub Tester()
Dim x As Boolean
Set x = search_bank("ctc", 38.4, True)
Debug.Print (x)
End Sub
And even declaring the variable before passing them in the tester (I'm not very used to VBA but for a moment I believed it was just so ancient, it needed things to be declared before they're passed)
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
bank_sheet? - BruceWayneFindmethods return valid objects. If the values are not found in row 1, then they'll return aNothing, which would raise this error later in your code. - David ZemensRange.Findcan returnNothing. You have to test the return values. - CominternSheet1 (Sheet1)- that'sCodeName (WorksheetName), whereCodeNameis a global-scopeWorksheetobject variable you get for free. You can control its name in the properties toolwindow (F4), by changing the(Name)property. - Mathieu Guindon