而不是通配符:
Sub color()
Dim myRange As Range, valuee As String
valuee = InputBox("Search String:")
If valuee = vbNullString Then Exit Sub
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
For Each myRange In Selection
If InStr(myRange.value, valuee) > 0 Then
myRange.Interior.ColorIndex = 3
End If
Next myRange
End Sub
我們也可以使用.Find方法。
編輯#1:
下面是使用.Find和.FindNext版本:
Sub color2()
Dim myRange As Range, valuee As String
valuee = InputBox("Search String:")
If valuee = vbNullString Then Exit Sub
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Set myRange = Selection.Find(what:=valuee, after:=Selection(1))
If myRange Is Nothing Then
MsgBox "no value"
Exit Sub
End If
myRange.Interior.ColorIndex = 3
st = myRange.Address(0, 0)
Do Until myRange Is Nothing
Set myRange = Selection.FindNext(after:=myRange)
If myRange.Address(0, 0) = st Then Exit Do
myRange.Interior.ColorIndex = 3
Loop
End Sub
嘗試'如果myRange.value LIKE 「*」 和值「* 「然後'。等於是二進制比較。 LIKE被稱爲*模式匹配*。 – Jeeped