Next
Stay
AutoMacro - VBA 代码生成器
学到更多
使用 [char list] 作为通配符
The example above can be modified slightly to allow us to use the question mark, in addition to a character list of allowed characters. The wildcard string can therefore be amended to “?[e-i]m” where the first character can be anything, the second character has to be a character between e and i and the last letter has to be the character “m”. Only 3 characters are allowed.
1
2
3
4
5
6
7
8
SubCharListTest()
DimxAsInteger
Forx=3To8
IfRange("B"&x).Value Like"?[e-i]m"Then
Range("B"&x).Font.Color=vbRed
EndIf
Nextx
EndSub
The result of this code would be:
Using the hash (#) Wildcard in VBA
The hash (#) wildcard replaces a single digit in a VBA string. We can match between 0 to 9.
1
2
3
4
5
6
7
8
9
10
SubCheckForNumber()
DimxAsInteger,yAsInteger
Forx=3To8
Fory=2To5
IfActiveSheet.Cells(x,y)Like"##"Then
ActiveSheet.Cells(x,y).Font.Color=vbRed
EndIf
Nexty
Nextx
EndSub
The code above will loop through all the cells in the Range (“B3:E8”) and will change the color of the text in a cell to RED if a double-digit number is found in that cell.
In the example below, the code will only change the number if the last number is a 9.
1
2
3
4
5
6
7
8
9
10
SubCheckFor9()
DimxAsInteger,yAsInteger
Forx=3To8
Fory=2To5
IfActiveSheet.Cells(x,y)Like"#9"Then
ActiveSheet.Cells(x,y).Font.Color=vbRed
EndIf
Nexty
Nextx
EndSub