我编写了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

最佳答案

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

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

关于excel - Excel VBA替换问号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40335527/

10-11 03:08