VBA macro to copy row data into columns

James2011

New Member
Joined
Apr 27, 2011
Messages
2
Hi, I'm looking for an excel macro to dynamically copy row data and to paste into columns

The dataset below is a simplified version as the real version has a over a hundred names and spans 5 years hence it needs a macro to automate.


Copy from Sheet 1

1605653155814.png



Paste into Sheet2

1605653287433.png



I've searched for hours online looking for solutions but nothing hits the spot, any suggestions please?

Thanks
James
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this:
Run this script from sheet1:
VBA Code:
Sub Copy_Data()
'Modified  11/18/2020  1:43:23 AM  EST
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim LastColumn As Long
Dim Lastrowa As Long
Lastrowa = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row

    For i = 2 To Lastrow
        LastColumn = Sheets(1).Cells(i, Columns.Count).End(xlToLeft).Column
      
        Sheets(2).Cells(Lastrowa, 1).Resize(LastColumn - 2).Value = Sheets(1).Cells(i, 1).Value
        Sheets(2).Cells(Lastrowa, 2).Resize(LastColumn - 2).Value = Sheets(1).Cells(i, 2).Value

        Sheets(1).Cells(1, 3).Resize(, LastColumn - 2).Copy
        Sheets(2).Cells(Lastrowa, 3).PasteSpecial xlPasteValues, Transpose:=True

        Sheets(1).Cells(i, 3).Resize(, LastColumn - 2).Copy
        Sheets(2).Cells(Lastrowa, 4).PasteSpecial xlPasteValues, Transpose:=True
        Lastrowa = Sheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
hence it needs a macro to automate
really?

look at this
SourceResult
NameTypeJan-20Feb-20Mar-20Apr-20May-20Jun-20NameTypeDateValue
JamesContractor102030405060JamesContractorJan-2010
JohnEmployee20406080100120JamesContractorFeb-2020
KarenEmployee306090120150180JamesContractorMar-2030
JamesContractorApr-2040
JamesContractorMay-2050
JamesContractorJun-2060
JohnEmployeeJan-2020
JohnEmployeeFeb-2040
JohnEmployeeMar-2060
JohnEmployeeApr-2080
JohnEmployeeMay-20100
JohnEmployeeJun-20120
KarenEmployeeJan-2030
KarenEmployeeFeb-2060
KarenEmployeeMar-2090
KarenEmployeeApr-20120
KarenEmployeeMay-20150
KarenEmployeeJun-20180

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Unpivot = Table.UnpivotOtherColumns(Source, {"Name", "Type"}, "Date", "Value")
in
    Unpivot
 
Upvote 0
Formula

Book1
ABCDEFGH
1NameType20-Jan20-Feb20-Mar20-Apr20-May20-Jun
2JamesContractor102030405060
3JohnEmployee20406080100120
4KarenEmployee306090120150180
Sheet1



Cell Formulas
RangeFormula
A2:A19A2=INDIRECT(ADDRESS((CEILING(ROW()-ROW(A$2)+1,6)/6)+1,COLUMN(A$2),1,1,"Sheet1"))
B2:B19B2=INDIRECT(ADDRESS((CEILING(ROW()-ROW($B$2)+1,6)/6)+1,COLUMN(Sheet2!B2),1,1,"Sheet1"))
C2:C19C2=TEXT((INDIRECT(ADDRESS(1,(MOD((ROW()-ROW($C$1))-1,6)+1)+2,1,1,"Sheet1"))),"dd-mmm")
D2:D19D2=INDIRECT(ADDRESS((CEILING(ROW()-ROW($D$2)+1,6)/6)+1,(MOD((ROW()-ROW($D$1))-1,6)+1)+2,1,1,"Sheet1"))
 
Upvote 0
Thank you for the responses, three completely different approaches so lots to think about though it probably depends on the amount of data. The formula method is best with relatively small amounts of data, the macro was ok posting 50,000 rows but struggled at 100,000 and in theory the power pivot approach could be simplest once you understand power pivots.
 
Upvote 0
Thank you for the responses, three completely different approaches so lots to think about though it probably depends on the amount of data. The formula method is best with relatively small amounts of data, the macro was ok posting 50,000 rows but struggled at 100,000 and in theory the power pivot approach could be simplest once you understand power pivots.
So you had 100,000 rows and 5 years of columns 12 columns per year for a total of 60 columns?
I never use Power Query.
 
Upvote 0
Another macro, which should be able to handle that amount of data
VBA Code:
Sub James()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   Ary = Sheets("Sheet1").Range("A2").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary) * UBound(Ary, 2), 1 To 4)
   
   For r = 2 To UBound(Ary)
      For c = 3 To UBound(Ary, 2)
         nr = nr + 1
         Nary(nr, 1) = Ary(r, 1)
         Nary(nr, 2) = Ary(r, 2)
         Nary(nr, 3) = Ary(1, c)
         Nary(nr, 4) = Ary(r, c)
      Next c
   Next r
   Sheets("sheet2").Range("A2").Resize(nr, 4).Value = Nary
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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