字符串比较使用像通配符不工作

时间:2016-02-08 17:51:24

标签: excel-vba wildcard sql-like vba excel

我在B列的某些单元格中有数据看起来像Id#加上一些数字,例如'Id#85'

所有其他单元格中都有名称FirstName LastName

我需要找到其中包含Id#的单元格,我正在尝试以下操作,但不能正常工作

由于

Sub test()
Dim ws As Worksheet
Dim i As Long

  Set ws = ThisWorkbook.Sheets("Elements")

  With ws
  For i = 2 To 180
    If .Cells(i, "B").Text Like "Id#" & "*" Then Do something
  Next i
End With

End Sub

1 个答案:

答案 0 :(得分:2)

试试这个:

Sub test()
    Dim ws As Worksheet
    Dim i As Long
    Set ws = ThisWorkbook.Sheets("Elements")

    With ws
        For i = 2 To 180
            If Left(.Cells(i, "B").Text, 3) = "Id#" Then
                .Cells(i, "C").Value = True
            End If
        Next i
    End With
End Sub
相关问题