Excel: VBA to Add Row Name to and Column Headers to Row

hpo2509

New Member
Joined
Nov 17, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone. I am working on transposing a Column headers into rows based on if the cells under sales columns are more than zero. I am doing it manually but takes me so much time.
If anyone can possible suggest any codes to do this? example below

Employee NameEmployee NumberManagerSales 1Sales 2Sales 3Sales 4Sales 5
Danica12345Jenny02202
Robert67890Robert12050

What I want to achieve is this.

Danica12345JennySales 22
Danica12345JennySales 32
Danica12345JennySales 52
Robert67890RobertSales 11
Robert67890RobertSales 22
Robert67890RobertSales 45

Appreciate so much any insights.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
What you are doing is called an unpivot an bread and butter for Power Query

• Click anywhere in your data set
• On the menu Date > From Table/Range (see image below)
• It will ask you if "Create Table" in My Table has headers should be ticked > hit ok
• PQ will fire up
• On the right remove the Changed type step (hit the x)
• Highlight the 1st 3 columns (Name -> Manager)
• Transform > Unpivot Columns > Unpivot Other Columns
• Click and cell in the Grid - Ctrl+A to select all
• Transform > Detect Data Type
• Home > Close & Load To > Pick where you want the ouput to go.

1675041645793.png
 
Upvote 1
Assum data is in sheet1, result is in sheet2
Right click on sheet1's name, viewcode, then paste below code into. Hit F5 to run code:
VBA Code:
Option Explicit
Sub test()
Dim lr&, lc&, i&, j&, k&, rng, arr(1 To 10000, 1 To 5)
With Sheets("Sheet1")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    rng = .Range("A1", .Cells(lr, lc)).Value
    For i = 2 To UBound(rng)
        For j = 4 To UBound(rng, 2)
            If rng(i, j) > 0 Then
                k = k + 1
                arr(k, 1) = rng(i, 1): arr(k, 2) = rng(i, 2): arr(k, 3) = rng(i, 3)
                arr(k, 4) = rng(1, j): arr(k, 5) = rng(i, j)
            End If
        Next
    Next
End With
With Sheets("Sheet2")
    .Range("A2").Resize(k, 5).Value = arr
End With
End Sub[ATTACH type="full"]84110[/ATTACH][ATTACH type="full"]84111[/ATTACH]
 

Attachments

  • Capture1.JPG
    Capture1.JPG
    147.7 KB · Views: 3
  • Capture2.JPG
    Capture2.JPG
    35.8 KB · Views: 4
Upvote 1
Solution
What you are doing is called an unpivot an bread and butter for Power Query

• Click anywhere in your data set
• On the menu Date > From Table/Range (see image below)
• It will ask you if "Create Table" in My Table has headers should be ticked > hit ok
• PQ will fire up
• On the right remove the Changed type step (hit the x)
• Highlight the 1st 3 columns (Name -> Manager)
• Transform > Unpivot Columns > Unpivot Other Columns
• Click and cell in the Grid - Ctrl+A to select all
• Transform > Detect Data Type
• Home > Close & Load To > Pick where you want the ouput to go.

View attachment 84108
thanks Alex, data is still in the same file so would like to have the VBA in the same file as well, tried the power query but for another source though.
 
Upvote 0
Assum data is in sheet1, result is in sheet2
Right click on sheet1's name, viewcode, then paste below code into. Hit F5 to run code:
VBA Code:
Option Explicit
Sub test()
Dim lr&, lc&, i&, j&, k&, rng, arr(1 To 10000, 1 To 5)
With Sheets("Sheet1")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    rng = .Range("A1", .Cells(lr, lc)).Value
    For i = 2 To UBound(rng)
        For j = 4 To UBound(rng, 2)
            If rng(i, j) > 0 Then
                k = k + 1
                arr(k, 1) = rng(i, 1): arr(k, 2) = rng(i, 2): arr(k, 3) = rng(i, 3)
                arr(k, 4) = rng(1, j): arr(k, 5) = rng(i, j)
            End If
        Next
    Next
End With
With Sheets("Sheet2")
    .Range("A2").Resize(k, 5).Value = arr
End With
End Sub[ATTACH type="full"]84110[/ATTACH][ATTACH type="full"]84111[/ATTACH]
Thanks @bebo021999 will try and let you know.
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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