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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Do you have any merged cells?
Is the sheet protected?
 
Upvote 0
Sheet is not protected and only merged cells are above the data on Supervisor Fri..allocation sheet has no merged cells in the destination
 
Upvote 0
Is your data in a structured table?
Do you have any filters already set on the sheet?
 
Upvote 0
Thanks a lot Fluff..it was the table layout, adjusted it and good to go
 
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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