簡體   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