var testing = getRegex("ab", "cd", "i");
console.log(testing);
// Output:
/ab(.+)cd/i
我想以这样的方式修改上面的代码:如果字母表存在于任何单元格中,则会忽略它。
例如,一个单元格可能包含"你好9811"然后它不应该突出显示。只能对单元格中的数字进行检查
phm包含如下数据:" 9811,7849"等。
答案 0 :(得分:0)
这是您的程序的修改版本。程序尝试将单元格的值转换为整数。只有成功执行此操作,才会将Activecell.Value
与number(j)
进行比较。
Sub Highlight()
...same code as yours...
Cells.Find("hello").Select
ActiveCell.Offset(1, 0).Select
Set rn = sh.UsedRange
k = rn.Rows.Count + rn.Row - 1
' ignore errors related to CInt conversion that will follow
On Error Resume Next
For x = 1 To k
For j = 0 To UBound(number)
' try to convert value to integer
TempNumber = CInt(ActiveCell.Value)
' if value was an integer, work on it
If Err.number = 0 Then
If ActiveCell.Value <> number(j) Then
Selection.Interior.Color = vbYellow
Else
Selection.Interior.ColorIndex = xlNone
Exit For
End If
End If
Next j
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next x
End Sub
根据需求变更编辑
Sub Test()
highlight ("9811,7849")
End Sub
Sub highlight(phm As Variant)
Dim w As Workbook
Dim sh As Worksheet
Dim x As Integer
Dim rn As Range
Dim k As Long
Dim number() As Integer
' newly added variables
Dim TempNumber As Integer
Dim phmInt As Variant
Dim phmFound As Boolean
If phm <> 0 Then
' split the numbers
phm = Split(phm, ",")
ReDim number(LBound(phm) To UBound(phm)) As Integer
Set sh = Worksheets("sheet1")
sh.Select
Cells.Find("Number Type").Select
Set rn = sh.UsedRange
k = rn.Rows.Count + rn.Row - 1
For i = 1 To k
On Error Resume Next
' try to check if active cell is an integer
' and proceed only if it is an integer
TempNumber = CInt(ActiveCell.Value)
If Err.number = 0 Then
On Error GoTo 0
' set phmFound to false and then see if
' active cell's value matches any item in phm array
phmFound = False
For Each phmInt In phm
If CInt(ActiveCell.Value) = CInt(phmInt) Then
phmFound = True
Exit For
End If
Next phmInt
' if active cell's value matched at least one item
' in phm array, don't colorize it. Otherwise colorize it
ActiveCell.Select
If phmFound Then
Selection.Interior.ColorIndex = xlNone
Else
Selection.Interior.Color = vbGreen
End If
End If
Err.Clear
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next i
End If
End Sub
修改强>
要求:输入9811和7848,因此不应突出显示此格式的任何单元格 - hello 9811,9811,7848,abc 7848 ...应突出显示除上述内容之外的任何其他内容的剩余单元格
子测试() 亮点(“9811,7848”) 结束子
Sub highlight(phm As Variant)
Dim w As Workbook
Dim sh As Worksheet
Dim x As Integer
Dim rn As Range
Dim k As Long
Dim number() As Integer
' newly added variables
Dim TempNumber As Integer
Dim phmInt As Variant
Dim phmFound As Boolean
If phm <> 0 Then
' split the numbers
phm = Split(phm, ",")
ReDim number(LBound(phm) To UBound(phm)) As Integer
Set sh = Worksheets("sheet1")
sh.Select
Cells.Find("Number Type").Select
Set rn = sh.UsedRange
k = rn.Rows.Count + rn.Row - 1
For i = 1 To k
' does the cell have the number we are looking for?
phmFound = False
For Each phmInt In phm
TempNumber = InStr(Trim(ActiveCell.Text), CStr(phmInt))
If TempNumber > 0 Then
' check if there is any number after phmint
If Not IsNumeric(Mid(Trim(ActiveCell.Text), TempNumber + Len(CStr(phmInt)), 1)) Then
phmFound = True
Exit For
End If
End If
Next phmInt
' if active cell's value matched at least one item
' in phm array, don't colorize it. Otherwise colorize it
ActiveCell.Select
If phmFound Then
Selection.Interior.ColorIndex = xlNone
Else
Selection.Interior.Color = vbGreen
End If
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next i
End If
End Sub
答案 1 :(得分:0)
尝试在代码中添加功能
例如
Public Function OnlyDigits(pInput As String) As String
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Global = True
.Pattern = "\D"
OnlyDigits = .replace(pInput, vbNullString)
End With
Set objRegExp = Nothing
End Function
这是完整的代码。
Sub highlight(phm As Variant)
Dim w As Workbook
Dim sh As Worksheet
Dim x As Integer
Dim rn As Range
Dim k As Long
Dim 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("Number Type").Select
Set rn = sh6.UsedRange
k = rn.Rows.count + rn.Row - 1
On Error Resume Next
For i = 1 To k
For j = LBound(Number) To UBound(Number)
Number(j) = CInt(phm(j))
If Err.Number = 0 Then
If Val(OnlyDigits(ActiveCell.Value)) = Number(j) Or IsEmpty(ActiveCell.Value) Then
Selection.Interior.ColorIndex = xlNone
Else
Selection.Interior.Color = vbGreen
Exit For
End If
End If
Next j
ActiveCell.Offset(1, 0).Select 'moves activecell down one row.
Next i
End If
ActiveWorkbook.Save
End Sub
Public Function OnlyDigits(pInput As String) As String
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
With objRegExp
.Global = True
.Pattern = "\D"
OnlyDigits = .replace(pInput, vbNullString)
End With
Set objRegExp = Nothing
End Function