简体   繁体   中英

For next loop in vba not executing all rows

I have wrote a programm to replace special character in cell with normal character(alphabets) I have written comments for each block in my programm. However before executing all rows, after second row it goes to next column

Sub special_char_Replace()
    Dim h As String
    Dim m, clm, rw As Integer
    Dim colspc As New Collection
    Dim valspc As New Collection

    'Below part makes collection of special character and its replacement values

    On Error Resume Next
    ThisWorkbook.Worksheets("Sheet2").Activate
    m = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To m
        colspc.Add Cells(i, 1)
        valspc.Add Cells(i, 2)
    Next

    'Activate destination workbook from which special characters to be replaced

    Workbooks("common file.xlsx").Worksheets("Sheet1").Activate
    LR = Cells(Rows.Count, "E").End(xlUp).Row

    'Below loop replaces special characters and inserts original value at 5th 'column aside

    For clm = 5 To 6
        For rw = 2 To LR
            For i = 1 To m

                On Error Resume Next
                h = Range(Cells(rw, clm), Cells(rw, clm)).Find(What:=colspc(i), after:=Range(Cells(rw, clm), Cells(rw, clm)), LookIn:=xlFormulas, Lookat:=xlPart).Address
                If h <> "" Then
                    Range(h).Offset(0, 5) = Range(h).Value
                    Range(Cells(rw, clm), Cells(rw, clm)).Replace What:=colspc(i), replacement:=valspc(i), Lookat:=xlPart, searchorder:=xlByColumns, MatchCase:=False
                End If
                h = ""
            Next i
        Next rw
    Next clm
End Sub

Thanks for everyone's support I made changes, instead of find function, I have defined two dimentional string cellval(I,J)

First I will save all cell values in this string with for loop cellval(I, j) = Cells(I, j).Value

After execution of replace command, below lines will check all string values with result

if there is changes it will relfect in output sheet

If cellval(I, j) <> Sheets(ws.Name).Cells(I, j).Value Then

Cells(I, j).Value = cellval(I, j)

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