Why is my loop skipping rows?

clc011

New Member
Joined
Jan 7, 2013
Messages
3
Purpose: Look at table in Parameters worksheet for find/replace parameters. Use these values to find/replace text in Sales worksheet.

Heres the parameters table in the Parameters worksheet

[TABLE="width: 320"]
<tbody>[TR]
[TD]IF
[/TD]
[TD]then
[/TD]
[TD]Entry Code
[/TD]
[/TR]
[TR]
[TD]WILL CALL (Winebridge)
[/TD]
[TD][/TD]
[TD]Wine Bridge
[/TD]
[/TR]
[TR]
[TD]STEWCH09
[/TD]
[TD]>>>
[/TD]
[TD]CH09S1@VLX
[/TD]
[/TR]
[TR]
[TD]STEWCS08
[/TD]
[TD]>>>
[/TD]
[TD]CS08S1@VLX
[/TD]
[/TR]
[TR]
[TD]STEWCS09
[/TD]
[TD]>>>
[/TD]
[TD]CS09S1@VLX
[/TD]
[/TR]
[TR]
[TD]STEWPN08
[/TD]
[TD]>>>
[/TD]
[TD]PN08S1@VLX
[/TD]
[/TR]
[TR]
[TD]STEWHCS07
[/TD]
[TD]>>>
[/TD]
[TD]HCS07A1@VLX
[/TD]
[/TR]
[TR]
[TD]STEWSCS10
[/TD]
[TD]>>>
[/TD]
[TD]CS10@VLX
[/TD]
[/TR]
[TR]
[TD]STE-07NCS6PK
[/TD]
[TD]>>>
[/TD]
[TD]NCS07S1@WB
[/TD]
[/TR]
[TR]
[TD]STE-08CS6PK
[/TD]
[TD]>>>
[/TD]
[TD]CS08S1@WB
[/TD]
[/TR]
[TR]
[TD]STE-08RRPN
[/TD]
[TD]>>>
[/TD]
[TD]PN08S1@WB
[/TD]
[/TR]
[TR]
[TD]STE-09CH6PK
[/TD]
[TD]>>>
[/TD]
[TD]CH09S1@WB
[/TD]
[/TR]
[TR]
[TD]STE-09HCS
[/TD]
[TD]>>>
[/TD]
[TD]HCS09A1@WB
[/TD]
[/TR]
[TR]
[TD]STE-09SLSB
[/TD]
[TD]>>>
[/TD]
[TD]SB09@WB
[/TD]
[/TR]
[TR]
[TD]STE-10SLCS
[/TD]
[TD]>>>
[/TD]
[TD]CS10@WB
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Here's the code. wsSales has already been defined and set as a global variable.

Code:
Sub SearchReplaceWithParameters()
    Dim rngParameters As range
    Dim rngRow As range
    
    Set rngParameters = ActiveWorkbook.Worksheets("Parameters").range("FindReplace")
    For Each rngRow In rngParameters.Rows
        'Replace text in Sales Orders
        If Not IsEmpty(rngRow.Cells(rngRow.Rows.Row, 1)) Then
            wsSales.Cells.Replace _
                What:=rngRow.Cells(rngRow.Rows.Row, 1), _
                Replacement:=rngRow.Cells(rngRow.Rows.Row, 3)
        End If
    Next
End Sub

Thanks, in advance.

Crystal
 
It starts at 2nd row and then the next time it's at the 4th row, etc. I've checked counters and they look good.

Purpose: Look at table in Parameters worksheet for find/replace parameters. Use these values to find/replace text in Sales worksheet.

Heres the parameters table in the Parameters worksheet

[TABLE="width: 320"]
<tbody>[TR]
[TD]IF[/TD]
[TD]then[/TD]
[TD]Entry Code[/TD]
[/TR]
[TR]
[TD]WILL CALL (Winebridge)[/TD]
[TD][/TD]
[TD]Wine Bridge[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[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]>>>[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Here's the code. wsSales has already been defined and set as a global variable.

Code:
Sub SearchReplaceWithParameters()
    Dim rngParameters As range
    Dim rngRow As range
    
    Set rngParameters = ActiveWorkbook.Worksheets("Parameters").range("FindReplace")
    For Each rngRow In rngParameters.Rows
        'Replace text in Sales Orders
        If Not IsEmpty(rngRow.Cells(rngRow.Rows.Row, 1)) Then
            wsSales.Cells.Replace _
                What:=rngRow.Cells(rngRow.Rows.Row, 1), _
                Replacement:=rngRow.Cells(rngRow.Rows.Row, 3)
        End If
    Next
End Sub

Thanks, in advance.

Crystal
 
Upvote 0
Crystal,

Welcome to the board.

The problem is your attempt to set a row counter as per ....

Cells(rngRow.Rows.Row,1)

As you loopthrough rngParameters, each rngRow itself has incremented its way down within rngParameters.
Thus no need to try and account for the row at all, just the column.

So try......

Code:
Sub SearchReplaceWithParameters()
    Dim rngParameters As Range
    Dim rngRow As Range
    
    Set rngParameters = ActiveWorkbook.Worksheets("Parameters").Range("FindReplace")
    For Each rngRow In rngParameters.Rows
        'Replace text in Sales Orders
        If Not IsEmpty(rngRow.Cells(1, 1)) Then
           wsSales.Cells.Replace _
                What:=rngRow.Cells(1, 1), _
                Replacement:=rngRow.Cells(1, 3)
        End If
    Next rngRow
End Sub

Hope that makes sense and helps.
 
Upvote 0
That worked perfectly. I thought I was looking at the values and the row counter seemed right. But I've nearly pulled my hair out on this one, so I probably was looking at the wrong var. It's been a while since I've done vba, so a little learning curve for me!

Thanks so much,
Crystal

Crystal,

Welcome to the board.

The problem is your attempt to set a row counter as per ....

Cells(rngRow.Rows.Row,1)

As you loopthrough rngParameters, each rngRow itself has incremented its way down within rngParameters.
Thus no need to try and account for the row at all, just the column.

So try......

Code:
Sub SearchReplaceWithParameters()
    Dim rngParameters As Range
    Dim rngRow As Range
    
    Set rngParameters = ActiveWorkbook.Worksheets("Parameters").Range("FindReplace")
    For Each rngRow In rngParameters.Rows
        'Replace text in Sales Orders
        If Not IsEmpty(rngRow.Cells(1, 1)) Then
           wsSales.Cells.Replace _
                What:=rngRow.Cells(1, 1), _
                Replacement:=rngRow.Cells(1, 3)
        End If
    Next rngRow
End Sub

Hope that makes sense and helps.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top