Here’s my code. I have bank data with the description in column D and some contain this merchant info for club. i want it to return value 1301 in column E once found. It is not giving an error but isn’t returning the value. I am new to VBA. I can’t figure out what’s wrong.
Sub bankmerch() ' ' bankmerch Macro '1. declare variables '2. find records that contain club and return value in E Dim Club As String Dim finalrow As Integer Dim i As Integer 'row counter Club = "5XXXXXXXXX17877" finalrow = Sheets("Bank detail").Range("D10000").End(xlUp).Row For i = 2 To finalrow If Cells(i, 4) = ("*" & Club & "*") Then Cells(i, 5).Value = "1301" End If Next i End Sub
here is my data example
06/12/2018 442.07 MerchantServices Daily Dep JUN 5XXXXXXXXX17877 816019288*5XXXXXXXXX17877 9000005503 5XXXXXXXXX17877 R00000091004354846212
If Cells(i, 4) Like "*" & Club & "*" Then
If you want to make your code look a bit better, consider this:
Sub Bankmerch() Dim Club As String Dim finalrow As Long Dim i As Long With ThisWorkbook.Worksheets("Bank detail") Club = "*5XXXXXXXXX17877*" finalrow = .Range("D10000").End(xlUp).Row For i = 2 To finalrow If .Cells(i, 4) Like Club Then .Cells(i, 5).Value = "1301" End If Next i End With End Sub
It references the worksheet and the workbook of every cell explicitly and the
* are added to the string.