简体   繁体   中英

Excel VBA Replacing Question Mark

I have VBA code written up to find and replace question marks in all worksheets in a workbook. However it is not working, can anyone help me out to see where did i go wrong?

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   

You're better off using Excel's in-range replace function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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