VBA loop not working

youngbella

New Member
Joined
Sep 27, 2020
Messages
19
Office Version
  1. 2010
Platform
  1. Windows
Hi, I'm new to VBA.

I did some coding to do copy and paste but in loop.

However, the second loop is not running. is there something wrong with my code?
Basically, I have a list in "sheet2" and I want to copy and paste it to "sheet1" sheet and skipping few rows for each list.
Can someone help me??

VBA Code:
Sub copypasteskip()

Dim sheet1 As Variant
Dim sheet2 As Variant
Dim endnumber As Integer
Dim finalrow As Variant
Dim i As Integer
Dim r As Integer


Set sheet1 = ThisWorkbook.Worksheets("June")
Set sheet2 = ThisWorkbook.Worksheets("ImportData")

endnumber = sheet1.Cells(Rows.Count, "A").End(xlUp).Row
finalrow = sheet2.Cells(Rows.Count, "D").End(xlUp).Row


For r = 11 To endnumber 'the list should paste starts at rows 11, cells "A"
        
        
        For i = 14 To finalrow 'the list need to be copy starts at rows 14,cells "D"
        
   
                sheet2.Cells(i, "D").End(xlDown).Copy
                sheet1.Cells(r, "A").PasteSpecial xlPasteValues
          
        
        Next i
        
       r = r + 7 'need to skips 7 rows for each list
    
Next r

End sub
 
Untested

VBA Code:
Sub copypasteskip2()

    Dim sheet1 As Worksheet
    Dim sheet2 As Worksheet
    Dim Finalrow2 As Long
    Dim ni As Long
    Dim nr As Long
  


    Set sheet1 = ThisWorkbook.Worksheets("June")
    Set sheet2 = ThisWorkbook.Worksheets("ImportData")

  
    Finalrow2 = Sheet3.Cells(Rows.Count, "D").End(xlUp).Row

    nr = sheet1.Cells(Rows.Count, "D").End(xlUp).Row + 8
      
    For ni = 14 To Finalrow2                     'the list need to be copy starts at rows 14,cells "D"
      

        sheet2.Cells(ni, "D").Copy
        sheet1.Cells(nr, "A").PasteSpecial xlPasteValues
        
        nr = nr + 8                              'need to skips 7 rows for each list
    Next i

End Sub
 
Last edited:
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Untested

VBA Code:
Sub copypasteskip2()

    Dim sheet1 As Worksheet
    Dim sheet2 As Worksheet
    Dim Finalrow2 As Long
    Dim ni As Long
    Dim nr As Long
 


    Set sheet1 = ThisWorkbook.Worksheets("June")
    Set sheet2 = ThisWorkbook.Worksheets("ImportData")

 
    Finalrow2 = Sheet3.Cells(Rows.Count, "D").End(xlUp).Row

    nr = sheet1.Cells(Rows.Count, "D").End(xlUp).Row + 8
     
    For ni = 14 To Finalrow2                     'the list need to be copy starts at rows 14,cells "D"
     

        sheet2.Cells(ni, "D").Copy
        sheet1.Cells(nr, "A").PasteSpecial xlPasteValues
       
        nr = nr + 8                              'need to skips 7 rows for each list
    Next i

End Sub
Dear Sir,

Thank you so much. I need to touch up a little bit as I want it to starts paste at row 11.
 
Upvote 0
Thank you so much. I need to touch up a little bit as I want it to starts paste at row 11.
It pastes 8 rows after your last data in column A which you have already set when you ran your first code, this code is for the 2nd (and subsequent) runs only.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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