繁体   English   中英

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

[英]Excel VBA: How to find a certain substring

在B列中,我有hmc1,hmc2,hmc3的数据。 我想查找包含“ hmc”的每个B列单元格,并用“ Found”替换其在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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM