Excel 如何将VBA中的what:=用于类似和多个条件
我有下面的代码,这是工作良好。但是,我需要知道如何使用like条件来代替什么:“,”和我需要查找的多个搜索条件(“,”-“,“*”,等等)Excel 如何将VBA中的what:=用于类似和多个条件,excel,vba,Excel,Vba,我有下面的代码,这是工作良好。但是,我需要知道如何使用like条件来代替什么:“,”和我需要查找的多个搜索条件(“,”-“,“*”,等等) 提前感谢您的帮助如评论中所述,您可以使用多个替换语句: Option Explicit Public Sub ReplaceDoTwComma() Dim findVal As Variant, replVal As Variant, i As Long, cols As Range findVal = Split(", - * tes
提前感谢您的帮助如评论中所述,您可以使用多个替换语句:
Option Explicit
Public Sub ReplaceDoTwComma()
Dim findVal As Variant, replVal As Variant, i As Long, cols As Range
findVal = Split(", - * test1") 'add items to replace separated by a space
replVal = Split(". _ ! test3") 'add replacements (same number as findVal)
With Worksheets("Sheet1")
With .Range(.Cells(1), .Cells.SpecialCells(xlCellTypeLastCell))
Set cols = Union(.Columns(4), .Columns(7))
End With
End With
For i = LBound(findVal) To UBound(findVal)
If findVal(i) = "*" Then findVal(i) = "~*" 'escapes special wild char "*"
cols.Replace What:=findVal(i), Replacement:=replVal(i), LookAt:=xlPart
Next
End Sub
你说的“类似条件”是什么意思?对于多个条件,您需要使用多个
。在VBA中为每个条件替换行。您可以使用通配符,如?
来表示单个字符,*
来表示字符串。您可以使用正则表达式将给定符号集中出现的所有符号替换为目标符号,尽管这需要使用循环迭代单元格,而不是使用Replace()
方法。谢谢您的输入
Option Explicit
Public Sub ReplaceDoTwComma()
Dim findVal As Variant, replVal As Variant, i As Long, cols As Range
findVal = Split(", - * test1") 'add items to replace separated by a space
replVal = Split(". _ ! test3") 'add replacements (same number as findVal)
With Worksheets("Sheet1")
With .Range(.Cells(1), .Cells.SpecialCells(xlCellTypeLastCell))
Set cols = Union(.Columns(4), .Columns(7))
End With
End With
For i = LBound(findVal) To UBound(findVal)
If findVal(i) = "*" Then findVal(i) = "~*" 'escapes special wild char "*"
cols.Replace What:=findVal(i), Replacement:=replVal(i), LookAt:=xlPart
Next
End Sub