Loop

TexEth

New Member
Joined
Jan 24, 2023
Messages
22
Office Version
  1. 365
Platform
  1. Windows
I need VBA code for the following copy and-paste looping task.
I am using Excel 365.

Problem
The first row is a header row.
Copy a block (range) of data from Sheets1 to Sheets2.
The range to be copied is always 22 rows and 32 columns.
The first block first and last cell: E2, AJ23
Paste special (transpose) on Sheets2.
Always special paste (transpose) on the first empty cell on E column.
Loop through the data until the last block.
The data starts at E2 and ends at AT2839

Thank you!!
 

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
This should work:
VBA Code:
Sub moveData()
  Application.ScreenUpdating = False
  For i = 2 To 2818 Step 22
    Worksheets("Sheet1").Cells(i, 5).Resize(22, 32).Copy
    Worksheets("Sheet2").Cells(Rows.Count, 5).End(xlUp).Offset(1).PasteSpecial Transpose:=True
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you.
I will test the code and get back to you.
Have a great day!!
 
Upvote 0
I need one more VB code.

In Sheets1, each country name is listed on Column A for 22 rows.
Example: Country 1: A2:A23
Country 2: A24: A45
So on.

Task:
Copy and paste country names on Sheets2, on column A, for 32 rows.
Example: Country 1: A2:A33
Country 2: A34: A65
Do this until the last country is copied and pasted the same way.
 

Attachments

  • Capture.PNG
    Capture.PNG
    47 KB · Views: 2
Upvote 0
This should work:
VBA Code:
Sub moveData()
  Application.ScreenUpdating = False
  For i = 2 To 2818 Step 22
    Worksheets("Sheet1").Cells(i, 5).Resize(22, 32).Copy
    Worksheets("Sheet2").Cells(Rows.Count, 5).End(xlUp).Offset(1).PasteSpecial Transpose:=True
  Next
  Application.ScreenUpdating = True
End Sub
The code was precise and efficient. Many thanks!!!!!
 
Upvote 0

Forum statistics

Threads
1,215,537
Messages
6,125,394
Members
449,222
Latest member
taner zz

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