Loop not breaking as expected

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I have the following code that I'm expecting to break out of the loop once it no longer sees a match on the customer name. But what I'm noticing is the loop continues until it exhausts all rows in the range. I've tried a few variations with no luck so far... How can I get that to work properly?

For k = 1 To LastRow(wsItemInfo)
Do While wsItemInfo.Cells(k, 1).Value = customerRow.Value
wsItemInfo.Cells(k, 2).Copy
wsCustomerReportCard.Range("A" & LastRow(wsCustomerReportCard) + 1).PasteSpecial xlPasteValues
wsItemInfo.Cells(k, 3).Copy
wsCustomerReportCard.Range("B" & LastRow(wsCustomerReportCard, "B") + 1).PasteSpecial xlPasteValues
wsItemInfo.Cells(k, 4).Copy
wsCustomerReportCard.Range("E" & LastRow(wsCustomerReportCard, "E") + 1).PasteSpecial xlPasteValues
k = k + 1
Loop
Next k
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Does this do what you want:

Code:
    k = 1
    Do While wsItemInfo.Cells(k, 1).Value = customerRow.Value
        wsItemInfo.Cells(k, 2).Copy
        wsCustomerReportCard.Range("A" & LastRow(wsCustomerReportCard) + 1).PasteSpecial xlPasteValues
        wsItemInfo.Cells(k, 3).Copy
        wsCustomerReportCard.Range("B" & LastRow(wsCustomerReportCard, "B") + 1).PasteSpecial xlPasteValues
        wsItemInfo.Cells(k, 4).Copy
        wsCustomerReportCard.Range("E" & LastRow(wsCustomerReportCard, "E") + 1).PasteSpecial xlPasteValues
        k = k + 1
    Loop
 
Upvote 0
Does this do what you want:

Code:
    k = 1
    Do While wsItemInfo.Cells(k, 1).Value = customerRow.Value
        wsItemInfo.Cells(k, 2).Copy
        wsCustomerReportCard.Range("A" & LastRow(wsCustomerReportCard) + 1).PasteSpecial xlPasteValues
        wsItemInfo.Cells(k, 3).Copy
        wsCustomerReportCard.Range("B" & LastRow(wsCustomerReportCard, "B") + 1).PasteSpecial xlPasteValues
        wsItemInfo.Cells(k, 4).Copy
        wsCustomerReportCard.Range("E" & LastRow(wsCustomerReportCard, "E") + 1).PasteSpecial xlPasteValues
        k = k + 1
    Loop

thanks igold, but no. it doesn't even enter the do while, it doesn't pick up on the value of wsItemInfo "A1".
 
Upvote 0
That really does not make sense. In both examples on the first loop through K=1.
 
Upvote 0
Hi Guys,

the trick was removing the for loop with k and initializing it as k = 2 (first data row). customerRow simply holds the name of the customer.

Thanks for the help.
 
Upvote 0
Glad you got it working. We were happy to help. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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