简体   繁体   中英

how can I use what:= in VBA for like and multiple condition

I have below code, which is working fine. However I need to know how to use like condition in place of what:',' and multiple search criteria like I need to find ("," "-", "*", etc)

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

Thanks in advance for help

As mentioned in the comments, you can use multiple Replace statements:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM