替换范围内的值

时间:2020-05-26 23:27:05

标签: excel vba replace

我尝试用两种不同的方法进行替换(第一次尝试在下面评论),但是最终替换了工作表上的所有内容,

Sub NoNullSaveCSV()
    Dim WB As Workbook
    Dim WS As Worksheet
    Dim find1 As Variant
    Dim rplc1 As Variant
    Dim find2 As Variant
    Dim rplc2 As Variant
    Dim Rng As Range

    Application.Workbooks.Add xlWBATWorksheet
    Set WB = ActiveWorkbook
    Set WS = ActiveSheet

    With ThisWorkbook.Worksheets("PedidosTratados")
        .Range("A3:DW1000").Copy
        WS.Range("A1").PasteSpecial xlPasteValues
    End With

    WS.Range("A1").Value = "FilterCol"
    WS.Columns.AutoFilter Field:=1, Criteria1:=""
    WS.UsedRange.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    WS.AutoFilterMode = False

    ReplaceCount = ReplaceCount + Application.WorksheetFunction.CountIf(WS.Cells, "*" & fnd1 & "*")
    '+ Application.WorksheetFunction.CountIf(WS.Cells, "*" & fnd2 & "*")

    fnd1 = "7.9000"
    rplc1 = "7.900"
    fnd2 = "9.9000"
    rplc2 = "9.900"

    With WS
        .Range("AT2:DW1000").Replace fnd1, rplc1
        .Range("AT2:DW1000").Replace fnd2, rplc2

        '.Cells.Replace what:=fnd1, Replacement:=rplc1, _
        'LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False, _
        'SearchFormat:=False, ReplaceFormat:=False

        '.Cells.Replace what:=fnd2, Replacement:=rplc2, _
        'LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False, _
        'SearchFormat:=False, ReplaceFormat:=False
    End With

        WB.SaveAs fileName:=ThisWorkbook.Path & "\Pedidos.csv", FileFormat:=xlCSV
        WB.Close False

MsgBox "Após a pesquisa, foram feitas " & ReplaceCount & " substituições."

End Sub

你能告诉我为什么这不能将替换限制在指定范围内吗?

谢谢!

0 个答案:

没有答案