Macro help: Transpose base on column

harky

Active Member
Joined
Apr 8, 2010
Messages
405
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
This is how my data is

O9pXhPq.jpg



This is the result
The Macro will Transpose COL AG (Font format had to retain (like colour, bold or underline)
ELGNzGk.jpg


I had get 1 bro to help me but the code will auto fill in other row..

VBA Code:
Sub Step2_Transpose2()

Dim Lst As Long, Lstcol As Long, n As Long

    'START of confirmation message box'
    response = MsgBox("Run Macro for" & vbNewLine & "Step 2: Transpose?", vbYesNo)
    If response = vbNo Then
    MsgBox ("Macro Canceled!")
    Exit Sub
    End If
    'END of confirmation message box'

Application.ScreenUpdating = False
Lst = Range("C" & Rows.Count).End(xlUp).Row '<- modify 'C' (refer to BS Code Cell)

For n = Lst To 4 Step -1
    Lstcol = Cells(n, Columns.Count).End(xlToLeft).Column - 33 '<- modify "- 6" to svc col (count from col A)
    With Cells(n, 1).EntireRow
        If Lstcol > 0 Then
            .Copy
            Cells(n + 1, 1).Resize(Lstcol).EntireRow.Insert shift:=xlDown
            Cells(n, 34).Resize(, Lstcol).Copy '<- modify '7', count from col A to svc col and +1 (eg. 6+1 =7)
            Cells(n + 1, 33).PasteSpecial Transpose:=True '<- modify '6' (count from col A to svc col)
        End If
End With
Next n

Columns("AH:AZ").EntireColumn.Delete  '<- Delete from Col G after transposed (modify if u change col)
Application.ScreenUpdating = True


        Columns.AutoFit
        Rows.AutoFit

    'START MSG'
      MsgBox "Transpose Completed!"
      Exit Sub
    'End MSG'


End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You could do an Unpivot in Power Query, but it will not keep the colors.
 
Upvote 0
PQ can handle million lines of data, but the choice is yours. Good luck seeking another answer.
 
Upvote 0
Hi Thanks. I manage to find my ans for my question.
remove .Copy

Code:
Sub Transpose()

Dim Lst As Long, Lstcol As Long, n As Long

    'START of confirmation message box'
    response = MsgBox("Run Macro for" & vbNewLine & "Step 2: Transpose?", vbYesNo)
    If response = vbNo Then
    MsgBox ("Macro Canceled!")
    Exit Sub
    End If
    'END of confirmation message box'

Application.ScreenUpdating = False

Lst = Range("D" & Rows.Count).End(xlUp).Row '<- modify 'D' (refer to BS Code Cell)

For n = Lst To 4 Step -1
    Lstcol = Cells(n, Columns.Count).End(xlToLeft).Column - 33 '<- modify "- 33" to svc col (count from col A)
    With Cells(n, 1).EntireRow
        If Lstcol > 0 Then

'           .Copy
            Cells(n + 1, 1).Resize(Lstcol).EntireRow.Insert shift:=xlDown
            Cells(n, 34).Resize(, Lstcol).Copy '<- modify '34', count from col A to svc col and +1 (eg. 6+1 =7)
            Cells(n + 1, 33).PasteSpecial Transpose:=True '<- modify '33' (count from col A to svc col)
        End If
End With
Next n
Columns("AH:AZ").EntireColumn.Delete  '<- Delete from Col G after transposed (modify if u change col)

Application.ScreenUpdating = True

    Columns.AutoFit
    Rows.AutoFit


    'START MSG'
     MsgBox "Transpose Completed!"
      Exit Sub
    'End MSG'

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,643
Members
449,093
Latest member
Ahmad123098

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