如何在VBA中使用:= for like和multiple condition

时间:2017-07-19 20:26:32

标签: excel-vba vba excel

我有以下代码,工作正常。但是,我需要知道如何使用类似的条件代替什么:','和我需要找到的多个搜索条件(","" - "," *"等)

Option Explicit
Sub ReplaceDoTwComma()   
        Worksheets("Sheet1").Columns("D").replace _
            What:=",", replacement:=".", LookAt:=xlPart    
        Worksheets("Sheet1").Columns("G").replace _
            What:=",", replacement:=".", LookAt:=xlPart 
End Sub

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如评论中所述,您可以使用多个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