Excel VBA替换问号

时间:2016-10-31 01:41:16

标签: excel vba excel-vba

我编写了VBA代码,用于查找和替换工作簿中所有工作表中的问号。然而它不起作用,任何人都可以帮我看看我哪里出错了?

Sub ReplaceQM()

    Dim lRow As Long
    Dim lCol As Long

    totalSheet = ThisWorkbook.Sheets.Count

    MsgBox totalSheet
    For x = 1 To totalSheet


        lRow = ThisWorkbook.Sheets(x).Cells(Rows.Count, 1).End(xlUp).Row
        lCol = ThisWorkbook.Sheets(x).Cells(1, Columns.Count).End(xlToLeft).Column


        For Z = 1 To lRow
            For i = 1 To lCol

                getPos = InStr(1, ThisWorkbook.Sheets(x).Cells(Z, i).Value, "~?")

                If getPos > 0 Then

                    ThisWorkbook.Sheets(x).Cells(Z, i).Value = Replace(ThisWorkbook.Sheets(x).Cells(Z, i).Value, "~?", " ")

                End If

            Next i
        Next Z
    Next x
End Sub   

1 个答案:

答案 0 :(得分:2)

最好使用Excel的范围内替换功能:

For Each ws In ThisWorkbook
  ws.UsedRange.Cells.Replace what:="~?", Replacement:=" ", LookAt:=False, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next ws