Excel VBA:如何查找某个子字符串

时间:2016-05-20 13:58:11

标签: excel vba excel-vba

在B栏中,我有数据,例如hmc1,hmc2,hmc3。我想找到包含" hmc"的每个B列单元格。并用"找到"替换A列中相应的单元格。 到目前为止,我的代码完全匹配,但如果匹配子字符串则不行。

        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 个答案:

答案 0 :(得分:2)

假设您的B列数据以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