VBA to convert data from Row description to Column Header

JmanSchu55

New Member
Joined
Mar 4, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I need help creating a Macro that will convert the data from my starting attached image, to the ending data. I manually pivot the data by filtering it by description, then do a formula for Hours and earnings to slide it over into the proper column. After that I save all formulas as values and remove the data from the original regular hour and amount column. I know there has to be a better way? The description column can have any number of pay types that are not always the same. I do this on many files, and would love a way to speed this process up. Let me know if possible, thank you in advance.
 

Attachments

  • End data.jpg
    End data.jpg
    137.3 KB · Views: 22
  • Starting data.jpg
    Starting data.jpg
    87.7 KB · Views: 23

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi and welcome to MrExcel!

Understanding that each of the descriptions in column C are found in row 1 starting in column F, try the following macro:
VBA Code:
Sub ConvertData()
  Dim i As Long
  Dim f As Range
  For i = 2 To Range("A" & Rows.Count).End(3).Row
    Set f = Range("E1:Z1").Find(Range("C" & i).Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      Cells(i, f.Column).Resize(1, 2).Value = Range("D" & i).Resize(1, 2).Value
    End If
  Next
End Sub
 
Upvote 0
You can use the following formula:
Dante Amor
ABCDEFGHIJKLM
1Emp IDCh DateDescReg HoursReg DollarsHourlyHourlyReported TipsReported TipsSalarySalaryTip HourlyTip Hourly
22607-eneHourly90259025      
3707-eneTip Hourly7288      7288
4707-eneReported Tips388  388    
5207-eneTip Hourly2224      2224
6207-eneReported Tips040  040    
7107-eneTip Hourly3744      3744
8807-eneReported Tips4314  4314    
91507-eneSalary4497    4497  
101707-eneHourly22882288      
111007-eneHourly66706670      
121007-eneTip Hourly8193      8193
131007-eneHourly79617961      
Start
Cell Formulas
RangeFormula
F2:M13F2=IF(F$1=$C2,IF(F$1<>E$1,$D2,$E2),"")


Or this simplified macro:
VBA Code:
Sub ConvertData_2()
  With Range("F2:M" & Range("A" & Rows.Count).End(3).Row)
    .Formula = "=IF(F$1=$C2,IF(F$1<>E$1,$D2,$E2),"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0
That works, is there a way to create the Headers from the description list? It would be "Description" Hours "Description"Amount for each Unique description in column C
 
Upvote 0
is there a way to create the Headers from the description list? It would be "Description" Hours "Description"Amount for each Unique description in column C
I recommend that you create the headers before the formulas or the macro, that way you will position them in the place that is convenient for you.
 
Upvote 0
is there a way to create the Headers from the description list? It would be "Description" Hours "Description"Amount for each Unique description in column C
Column names will be created in order of appearance.

Try this:

VBA Code:
Sub ConvertData()
  Dim i As Long, lc As Long
  Dim f As Range
  For i = 2 To Range("A" & Rows.Count).End(3).Row
    Set f = Range("E1:Z1").Find(Range("C" & i).Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      lc = f.Column
    Else
      lc = Cells(1, Columns.Count).End(1).Column + 1
      Cells(1, lc).Resize(1, 2).Value = Range("C" & i).Value
    End If
    Cells(i, lc).Resize(1, 2).Value = Range("D" & i).Resize(1, 2).Value
  Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,781
Messages
6,126,870
Members
449,345
Latest member
CharlieDP

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