How to stop looping when it returns to the start of the loop

Iceman63

New Member
Joined
Dec 9, 2019
Messages
23
Office Version
  1. 365
Platform
  1. Windows
hi folks,
I wrote the below code to loop through a column on a sheet and copy the row information and paste it on the destination sheet. When I run the code, it loops and pastes the information on the destination sheet, but it continues to loop and overwrites the information on the destination sheet. How do I stop this, and tell the code to stop when it is done? Thanks

VBA Code:
Option Explicit

Sub Transfer()
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim myname As String

lastrow1 = Sheets("Supervisor Fri").Range("A" & Rows.Count).End(xlUp).Row

    For i = 14 To lastrow1
        myname = Sheets("Supervisor Fri").Cells(i, "a").Value

    Sheets("Shop allocation").Activate
        lastrow2 = Sheets("Shop allocation").Range("a" & Rows.Count).End(xlUp).Row

For j = 11 To lastrow2
    If Sheets("Shop allocation").Cells(j, "A").Value = myname Then
        Sheets("Supervisor Fri").Activate
        Sheets("Supervisor Fri").Range(Cells(i, "c"), Cells(i, "f")).Copy
        Sheets("Shop allocation").Activate
        Sheets("Shop allocation").Range(Cells(j, "b"), Cells(j, "E")).Select

ActiveSheet.Paste
            End If

        Next j
        Application.CutCopyMode = False

    Next i
        Sheets("Supervisor Fri").Activate
        Sheets("Supervisor Fri").Range("A14").Select




End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Can you please explain what you mean by
but it continues to loop and overwrites the information on the destination sheet.
Also can the "myname" value be found multiple times on either sheet?
 
Upvote 0
I'm going to take a wild guess here (because I've done something similar in the past, and this seemed familiar to me) that you have 13 rows of "header" information on the worksheet, which is why you started your row counter at i = 14. From there you set the loop = total rows in column A:A. (Consider that Rows.Count is going to include those original 13 rows.) So if you start an operation from Row 14 and do it "Rows.Count" times, then the final 13 are probably unwanted. See if adjusting your row count constraint does the trick for you.

Alternatively, you can break out of loops with a GoTo command to force the code to operate from another point when the GoTo constraint or limit is reached. (It's not the best way to do that, but it does work.)
 
Upvote 0
Thanks..in column A on both sheets is a string, the string can vary from number, text or a combination of both. I put 38 in column A on the Supervisor Fri sheet, with blanks in between. On the shop allocation sheet, column A has 38 assigned to each row starting at row 11. The friday sheet could have upto 7 different shop locations. I was trying to loop through and copy the row on Fri sheet with 38 next to it, but when it runs it loops many times. And yes the value can be found on both sheets many times.
 
Upvote 0
In that case can you please post some sample data from both sheets using the XL2BB add-in and explain what should happen.
 
Upvote 0
The is the Friday sheet where the data comes from
Book1
ABCDEF
13Shop LocationAsset ID.Work Order #CommentsWork Order Hours
1438SCP-151277674PM SEMI-ANN. FSS ***4
1538SCP-151263695PM 500 HR ***24
1638SCP-138 279393PM REPAIRS40
1738SCP-194277625FULL INSPECTION EVERY 6 MONTHS FIRE SUPPRESSION SYSTEM4
1838P702280617250 HOUR PM8
1938JUM-005280614PM 500 HOUR BOOM (MECH) ***24
20TRK-214281963PRE-PM INSPECTION1
21TRK-208281965PRE-PM INSPECTION1
22SCP-172 281122boom repair20
23SCP-172 280400repairs8
24SCP-172 279358HOIST CYL LEAK, PINS, BUSHING4
25SCP-172 277898See Notes 1
26SCP-172 275640OIL LEAK MID SHIP AREA1
27SCP-172 275607BRAKES / LEAK1
28SCP-172 274768Dump cylinder pin & Trans Leak (5450)1
Supervisor Fri
 
Upvote 0
This is the destination sheet
Book1
ABCDEF
10A SHOP
1138LTM340 2695372
1238LTM340 2695372
1338LTM340 2695372
1438LTM340 2695372
1538LTM340 2695372
1638LTM340 2695372
1738LTM340 2695372
1838LTM340 2695372
1938LTM340 2695372
2038LTM340 2695372
22B SHOP
Shop allocation
 
Upvote 0
Explanation: There are 7 different shop locations, that could be used on any day of the week for work. (Yes there are 6 more sheets with the rest of the days).
I was trying to loop through column A on the friday sheet and copy the data over to the destination sheet from B,C,D and E. I was just trying out Friday, to see how it worked. So in column A on friday or any other day, there could be 7 different shop locations.
 
Upvote 0
So you want to copy each row once to the allocation sheet?
In your example you have 6 rows with 38 on the Friday sheet, but 10 on the allocation sheet. What should happen with those extra rows?
 
Upvote 0
Yes..
There are 4 more 38's at the bottom of the sheet, the column is actually 94 rows long, with not all of them used. The extra row's might have another shop location in them..
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

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