在我的GUI中,我输入这样的数字:9811,7841,它将被发送到我的宏。我的宏是:
sub highlight(fm as variant)
dim sh as worksheet
Dim i As Integer
dim j as integer
dim k As Long
Dim rn As Range
din number() as integer
If phm <> 0 Then
phm = Split(phm, ",")
ReDim number(LBound(phm) To UBound(phm)) As Integer
Set sh = w.Worksheets("Sheet1")
sh.Select
Cells.Find("Type").Select
ActiveCell.Offset(1, 0).Select
Set rn = sh.UsedRange
k = rn.Rows.Count + rn.Row - 1
For i = 1 To k
For j = LBound(number) To UBound(number)
number(j) = CInt(phm(j))
If ActiveCell.Value = number(j) Or IsEmpty(ActiveCell.Value) Then
Selection.Interior.ColorIndex = xlNone
Else
Selection.Interior.Color = vbGreen
Exit For
End If
Next j
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next i
End If
ActiveWorkbook.Save
End Sub
我想修改我的代码,以便在任何单元格中都存在字母被忽略。在下面的例子中,cell3和cell 5应该突出显示,因为我的“fm”包含9811,7841所以单元格1,2,4是有效的。检查单元格时,如果有的话,应忽略Alpha表格。
Sheet1
cell 1: 9811
cell 2: hello 9811
cell 3: 3428
cell 4: hello 7841
cell 5:hello 2545
答案 0 :(得分:0)
我认为您正在寻找VBA功能“Instr”
https://msdn.microsoft.com/en-us/en-en/library/8460tsh1%28v=vs.90%29.aspx
假设phm是你的数组,每个单元格中包含一个fm:
你需要改变你的行
If ActiveCell.Value = number(j) Or IsEmpty(ActiveCell.Value) Then
到
If Instr(ActiveCell.Value,number(j)) > 0 Or IsEmpty(ActiveCell.Value) Then
答案 1 :(得分:0)
最简单的方法是使用正则表达式。添加对Microsoft VBScript正则表达式的引用,然后只进行模式替换:
Private Function StripNonNumerics(inValue As String) As String
Dim regex As New RegExp
With regex
.Pattern = "\D"
.Global = True
StripNonNumerics = .Replace(inValue, vbNullString)
End With
End Function
请注意,如果将其合并到sub中或使正则表达式成为全局变量,则开销会减少(这样您就不必重复创建RegExp对象。