VBA-Macro Help with copy data from 1sheet into another sheet multiple times

kishu111

New Member
Joined
May 14, 2022
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2016
  4. 2010
  5. 2007
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi Mr. Excel,

I need a logic that would copy Sheet1 (Data Table) into Sheet2

1652567602884.png


Sheet2 (End Result should look like this)
In this sheet, MaterialA should be repeated 13 times and each of the months values should be copied vertically against the respective month.
I needed help on how to do this in VBA? (simplest possible way, so this sheet can be reused.)
1652567680656.png


Thank you in advance for all or any help you can provide.

Regards,

Kishu111
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi Mr. Excel,

I need a logic that would copy Sheet1 (Data Table) into Sheet2

View attachment 64682

Sheet2 (End Result should look like this)
In this sheet, MaterialA should be repeated 13 times and each of the months values should be copied vertically against the respective month.
I needed help on how to do this in VBA? (simplest possible way, so this sheet can be reused.)
View attachment 64683

Thank you in advance for all or any help you can provide.

Regards,

Kishu111
Wanted to see if anyone could please help with this?
 
Upvote 0
See if this works for you:
VBA Code:
Sub TransposeData()

    Dim srcSht As Worksheet, destSht As Worksheet
    Dim srcRng As Range, destRng As Range
    Dim srcArr As Variant
    Dim destArr() As Variant
    Dim i As Long, j As Long, destCnt As Long
    Dim sItem As String, sMth As String
    
    Set srcSht = Worksheets("Data")             '<-- Change to your sheet name
    Set destSht = Worksheets("Result")          '<-- Change to your sheet name
    Set srcRng = srcSht.Range("A1").CurrentRegion
    Set destRng = destSht.Range("A1")
    srcArr = srcRng.Value
    destCnt = 0
    
    ReDim destArr(1 To UBound(srcArr, 1) * UBound(srcArr, 2), 1 To 3)
    
    For i = 2 To UBound(srcArr)
        sItem = srcArr(i, 1)
        For j = 1 To 13
            destCnt = destCnt + 1
            sMth = srcArr(1, j + 1)
            destArr(destCnt, 1) = sItem
            destArr(destCnt, 2) = sMth
            destArr(destCnt, 3) = srcArr(i, j + 1)
        Next j
    Next i

    destRng.Offset(1).Resize(UBound(destArr, 1), UBound(destArr, 2)).Value = destArr
    destRng.Resize(1, 3).Value = Array("Material", "Volume Per", "Value")
    destRng.Resize(1, 3).Font.Bold = True
    destRng.CurrentRegion.Columns.AutoFit
    
End Sub
 
Upvote 0
Try this faster code
VBA Code:
Sub TransposeData()
Dim LR1 As Long, T As Long
Dim A, B
Application.ScreenUpdating = False
With Sheets("Sheet1").Range("A1")
A = .CurrentRegion.Offset(0, 1)
LR1 = .Offset(1, 0).End(xlDown).Row
B = .Range("A2:A" & LR1)
End With

With Sheets("Sheet2")
.Range("A1").CurrentRegion.Clear
.Range("A1:C1").Value = Array("Material", "Volume for", "Value")
    For T = 1 To LR1 - 1
    .Range("A2").Offset(12 * (T - 1), 0).Resize(12, 1).Value = B(T, 1)
    .Range("B2").Offset(12 * (T - 1), 0).Resize(12, 1).Value = WorksheetFunction.Transpose(WorksheetFunction.Index(A, 1, 0))
    .Range("C2").Offset(12 * (T - 1), 0).Resize(12, 1).Value = WorksheetFunction.Transpose(WorksheetFunction.Index(A, T + 1, 0))
    Next T
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Improved Code
VBA Code:
Sub TransposeData()
Dim LR1 As Long, T As Long
Dim A, B, C
Application.ScreenUpdating = False
With Sheets("Sheet1").Range("A1")
LR1 = .Offset(1, 0).End(xlDown).Row
A = .Range("A2:A" & LR1)
B = WorksheetFunction.Transpose(.Range("B1:M1"))
C = WorksheetFunction.Transpose(.Range("B2:M" & LR1))
End With

With Sheets("Sheet2")
.Range("A1").CurrentRegion.Clear
.Range("A1:C1").Value = Array("Material", "Volume for", "Value")
    
    For T = 1 To LR1 - 1
    With .Range("A" & 2 + (12 * (T - 1)))
    .Resize(12, 1).Value = A(T, 1)
    .Offset(0, 1).Resize(12, 1).Value = B
    .Offset(0, 2).Resize(12, 1).Value = WorksheetFunction.Index(C, 0, T)
    End With
    Next T
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Try this faster code
That was a pretty bold and untested statement.
Logically your code was never going to be faster.
The original code had 1 read and 1 write.
Your code has 1 read and "LR-1" writes ie For T = 1 To LR1 - 1

On 2,000 lines my code took < 0.2 secs. Your latest code 10 secs.
At 20k records my code took 1.8 secs, after about 5k your code takes that long that I ended up killing it.

PS: Although I agree that I wouldn't want the total in my output, the original sample indicated it wanted the Annual Total, you are not picking up that column.
(I just noticed that I didn't rename it and it is still called Grand Total in my output)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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