简体   繁体   English

如何在VBA中将what:=用于相同条件和多种条件

[英]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: 如注释中所述,您可以使用多个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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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