Transposing cells using macro and adding additional data

Xlitup

New Member
Joined
Jan 16, 2018
Messages
22
Hey Guys, in a bit of a pickle. How do I transpose cells from Columns to rows using a macro and also include the Asset Number and Date next to its respective element.

Asset NumberDateIronCrNickelAlPbCuSilver
TA2012/12/201824252652458565
TA2210/04/20186587545895452521

<tbody>
</tbody>



ElementValueAsset NumberDate
Iron24TA2012/12/2018
Cr25TA2012/12/2018
Nickel26TA2012/12/2018
Al52TA2012/12/2018
Pb45TA2012/12/2018
Cu85TA2012/12/2018
Silver65TA2012/12/2018
Iron65TA2210/04/2018
Cr875TA2210/04/2018
Nickel45TA2210/04/2018
Al895TA2210/04/2018
Pb45TA2210/04/2018
Cu25TA2210/04/2018
Silver21TA2210/04/2018

<tbody>
</tbody>

Don't know exactly how to explain the title but hopefully the example helps.
Thanks for helping in advance.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Using Power Query here is the Mcode for you.

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Asset Number", "Date"}, "Attribute", "Value"),
    #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Element"}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Element", "Value", "Asset Number", "Date"})
in
    #"Reordered Columns"
 
Upvote 0
@alansidman thanks for the quick response but any other way than using Power Query.

Cheers.
 
Upvote 0
@alansidman Sorry mate just tried using Power Query and worked like a charm. You absolute legend.

Cheers.
 
Upvote 0
Code:
Option Explicit


Sub TransChem()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim lr As Long, lr2 As Long
    Dim i As Long, arr As Variant
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Application.ScreenUpdating = False
    lr = s1.Range("A" & Rows.Count).End(xlUp).Row
    arr = Array("Asset Number", "Date", "Element", "Value")
    s2.Range("A1:D1") = arr
    For i = 2 To lr
        lr2 = s2.Range("C" & Rows.Count).End(xlUp).Row
        s1.Range("A" & i & ":B" & i).Copy s2.Range("A" & lr2 + 1)
        s1.Range("C1:I1").Copy
        s2.Range("C" & lr2 + 1).PasteSpecial xlPasteValues, , , True
        s1.Range("C" & i & ":I" & i).Copy
        s2.Range("D" & lr2 + 1).PasteSpecial xlPasteValues, , , True
    Next i
    lr2 = s2.Range("C" & Rows.Count).End(xlUp).Row
    For i = 3 To lr2
        If s2.Range("A" & i) = "" Then
            s2.Range("A" & i) = s2.Range("A" & i - 1)
            s2.Range("B" & i) = s2.Range("B" & i - 1)
        End If
    Next i
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    MsgBox "Complete"


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,732
Messages
6,126,540
Members
449,316
Latest member
sravya

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