首页 > 解决方案 > 尝试使用 Range.Replace 查找和替换 qoute 符号时出错

问题描述

我有一个宏文件,它可以拆分文件并按特定列表命名它们未创建此列表中包含引号的文件的名称,"我只想用该范围内的常规空间查找和替换所有文件。

但是当我运行宏时它会返回我

参数不是可选的。

有人能找到我的问题在哪里吗?

'Loop for creating files
For i = 2 To FilesCounter
    NewWBName = EmailWS.Cells(i, 1).Value
    Set NewWB = Workbooks.Add

    With SplitWS
        .Range(.Cells(1, 1), .Cells(LastRow, LastColumn)).AutoFilter Field:=1, Criteria1:=NewWBName
        .Range(.Cells(1, 2), .Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Copy  'Copy cells starting from column B
    End With

    With NewWB.Worksheets(1)
        .Cells(1, 1).PasteSpecial xlPasteFormats
        .Cells(1, 1).PasteSpecial xlPasteValues
        .Cells.EntireColumn.AutoFit
    End With

    'find and replace all th " in the WB names list
    With EmailWS
        .Range("$A:$A").Replace What:=""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False"
        .Cells(i, 1).Value = NameForFile & "" & NewWBName
    End With

    NewWB.SaveAs fileName:=DesPath & "\" & NameForFile & "" & NewWBName & ".xlsx", Password:=PassFile
    NewWB.Close

Next i

标签: excelvba

解决方案


"在 VBA 中可以写成""""Chr(34)

"""不一样""""

在即时窗口中输入这个

?Chr(34)
"    '<~~ This is what you will get

然后这个

?"""" = Chr(34)
True '<~~ This is what you will get

现在试试

?""" = Chr(34)

看看你得到了什么:)

在您的代码中,用"""or""""替换Chr(34)它变成What:=""""


推荐阅读