Excel VBA:如何find一个特定的子string

在B列中,我有例如hmc1,hmc2,hmc3的数据。 我想find包含“hmc”的每个列B单元格,并将列A中的相应单元格replace为“find”。 我的代码到目前为止工作,如果它完全匹配,但不是如果匹配一个子string。

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 

假设你的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 

在这里输入图像说明