0

In column B, I have data for e.g. hmc1, hmc2, hmc3. I want to find every Column B cell that contains "hmc" and replace its corresponding cell in Column A with "Found". My code so far works if it matches fully, but not if it matches a substring.

        If .Range("B" & r).Value = "hmc " Then
            .Range("A" & r).Value = "Found" 

Col A   Col B
Accept  hmc1
123     hmc1
123     hmc2
Accept  xcc
Accept  xcc
Accept  xcc
Accept  xcc
Accept  xcc
Accept  xcc
Accept  xcc
Accept  xcc
Accept  xcc
Accept  xcc
123     hmc3
Accept  hmc3
Accept  hmc3
1
  • 1
    InStr ? Commented May 20, 2016 at 14:06

1 Answer 1

2

Assuming your B column data starts with 2

Sub test()
    Dim lastrow As Long
    lastrow = Range("B" & Rows.Count).End(xlUp).Row
    For i = 2 To lastrow
        If InStr(LCase(Range("B" & i).Value), "hmc") Then
            Range("A" & i).Value = "Found"
        End If
    Next i
End Sub

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

this is it! Does LCase mean Lower case? How about if Col B, may contain example Hmc, HMC, hmc or 110 Hmc, which i dont care as long as it contains the keyword hmc. will it work? tq
it will work for HMC1 or Hmc1 or hMc1 etc., it will converts to lcase and check with lowercase hmc
i tried it works perfectly. thanks a lot, you are really expert!
If an answer helped you, don't forget to mark it as correct by clicking the check mark next to that answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.