Louis_Guionneau
New Member
- Joined
- Nov 12, 2019
- Messages
- 28
Hi Everyone,
Firstly, this forum is really great. I generally do my utmost not to post a new thread and read through other materials and try to work it out when I can. Here are some pre-existing materials on this topic that I already read through.
https://www.mrexcel.com/forum/excel-questions/733085-convert-matrix-data-list.html
https://nature.berkeley.edu/~oboyski67/download/MatrixConvert.txt
http://nhsexcel.com/excel-pivot-table-crosstab-flat-list/
https://codereview.stackexchange.co...-in-rows-and-columns-to-rows-in-vba-for-excel
https://www.mrexcel.com/forum/excel-questions/952004-syntax-error-crosstab-flat-file-macro.html
I'm pretty inexperienced with VBA (but trying to learn) so appreciate your patience as I ask what are probably really dumb questions.
I'm trying to develop a tool in VBA that will be able to dynamically adapt datasets in matrix format (potentially with multiple row headers) into a database / list format. For example, I recently came across this dataset (see exhibit 1) that I would want to be able to convert to the format shown in exhibit 2.
Exhibit 1
[TABLE="width: 517"]
<colgroup><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]
[/TD]
[TD]Bookings[/TD]
[TD]Bookings[/TD]
[TD]4 Wall[/TD]
[TD]4 Wall[/TD]
[TD]Metric[/TD]
[/TR]
[TR]
[TD]Customer ID[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD]Period[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD] $ 38K[/TD]
[TD] $ 31K[/TD]
[TD] $ 120K[/TD]
[TD] $ 29K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD] $ - [/TD]
[TD] $ - [/TD]
[TD] $ 9K[/TD]
[TD] $ 0K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD] $ 25K[/TD]
[TD] $ 56K[/TD]
[TD] $ 37K[/TD]
[TD] $ 35K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD] $ - [/TD]
[TD] $ 25K[/TD]
[TD] $ - [/TD]
[TD] $ 21K[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Exhibit 2
[TABLE="width: 365"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]Customer ID[/TD]
[TD]Metric[/TD]
[TD]Period[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ 38K[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ 31K[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ 120K[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 29K[/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ 9K[/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 0K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ 25K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ 56K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ 37K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 35K[/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ 25K[/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 21K[/TD]
[/TR]
</tbody>[/TABLE]
I hope this makes sense so far! I'm excited to try and build this on my own but am coming up against some roadblocks so would really appreciate any brief tips! (But don't write it for me! I want to try and understand :D :D, thank you senseis).
I've come up with the below so far. My thinking is that I'll
1) get the user to define the input data / row data / number of column (headers) / column data.
2) Then I'll transpose (is this the best way?) the old headers and make them become rows.
3) Stack all the values
4) Do a count of the number of old header columns (e.g., how many total non-unique dates were there) and then copy each Customer ID that number of times and list these out as rows to align with the right column headers (which will now each be rows).
I started out writing the below. You'll see how bad I am at VBA quite quickly. Prepare yourself! My question is:
1) I tried to do step 4) above in my last line of code (I know it's not in the order as listed above) but I thought it would be an easier step but my count isn't working - can you help guide me?
2) What is the best way to copy and paste all the values? Should I use a transpose function?
I'd really appreciate any tips! Please don't feel you have to write the code for me. You guys are so good at this and I really want to learn.
Thank you so much!
Louis
Sub LG_Data_Converter()
'Part 1 - Definitions
Dim Input_Range As Range
Dim Output_Range As Range
Dim Row_ID As Range
Dim Record_Descriptor_1 As Range
Dim Record_Descriptor_2 As Range
Set Input_Range = Application.InputBox( _
"Make Selection", "Select Input Range", Type:=8, Default:=Selection.CurrentRegion.Address)
Set OutputRange = Application.InputBox(prompt:="Select Output Location", Type:=8, Default:=Selection.CurrentRegion.Address)
Set Row_ID = Application.InputBox("Make Selection", "Select Record Label (Row Titles)", Type:=8, _
Default:=Selection.CurrentRegion.Address)
Set Record_Descriptor_1 = Application.InputBox( _
"Make Selection", "Select Record Descriptor #1 (#1 Column Title)", Type:=8, Default:=Selection.CurrentRegion.Address)
Set Record_Descriptor_2 = Application.InputBox( _
"Make Selection", "Select Record Descriptor #2 (#2 Column Title", Type:=8, Default:=Selection.CurrentRegion.Address)
'Part 2 - Conversion Process
Dim Count As Long
Count = Cells(Range(Record_Descriptor_1)).End(xlToRight).Columns
Firstly, this forum is really great. I generally do my utmost not to post a new thread and read through other materials and try to work it out when I can. Here are some pre-existing materials on this topic that I already read through.
https://www.mrexcel.com/forum/excel-questions/733085-convert-matrix-data-list.html
https://nature.berkeley.edu/~oboyski67/download/MatrixConvert.txt
http://nhsexcel.com/excel-pivot-table-crosstab-flat-list/
https://codereview.stackexchange.co...-in-rows-and-columns-to-rows-in-vba-for-excel
https://www.mrexcel.com/forum/excel-questions/952004-syntax-error-crosstab-flat-file-macro.html
I'm pretty inexperienced with VBA (but trying to learn) so appreciate your patience as I ask what are probably really dumb questions.
I'm trying to develop a tool in VBA that will be able to dynamically adapt datasets in matrix format (potentially with multiple row headers) into a database / list format. For example, I recently came across this dataset (see exhibit 1) that I would want to be able to convert to the format shown in exhibit 2.
Exhibit 1
[TABLE="width: 517"]
<colgroup><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]
[/TD]
[TD]Bookings[/TD]
[TD]Bookings[/TD]
[TD]4 Wall[/TD]
[TD]4 Wall[/TD]
[TD]Metric[/TD]
[/TR]
[TR]
[TD]Customer ID[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD]Period[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD] $ 38K[/TD]
[TD] $ 31K[/TD]
[TD] $ 120K[/TD]
[TD] $ 29K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD] $ - [/TD]
[TD] $ - [/TD]
[TD] $ 9K[/TD]
[TD] $ 0K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD] $ 25K[/TD]
[TD] $ 56K[/TD]
[TD] $ 37K[/TD]
[TD] $ 35K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD] $ - [/TD]
[TD] $ 25K[/TD]
[TD] $ - [/TD]
[TD] $ 21K[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Exhibit 2
[TABLE="width: 365"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]Customer ID[/TD]
[TD]Metric[/TD]
[TD]Period[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ 38K[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ 31K[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ 120K[/TD]
[/TR]
[TR]
[TD="align: right"]1052[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 29K[/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ 9K[/TD]
[/TR]
[TR]
[TD="align: right"]1105[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 0K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ 25K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ 56K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ 37K[/TD]
[/TR]
[TR]
[TD="align: right"]1110[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 35K[/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]Bookings[/TD]
[TD="align: right"]7/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]Bookings[/TD]
[TD="align: right"]8/1/2014[/TD]
[TD] $ 25K[/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]11/1/2014[/TD]
[TD] $ - [/TD]
[/TR]
[TR]
[TD="align: right"]1123[/TD]
[TD]4 Wall[/TD]
[TD="align: right"]12/1/2014[/TD]
[TD] $ 21K[/TD]
[/TR]
</tbody>[/TABLE]
I hope this makes sense so far! I'm excited to try and build this on my own but am coming up against some roadblocks so would really appreciate any brief tips! (But don't write it for me! I want to try and understand :D :D, thank you senseis).
I've come up with the below so far. My thinking is that I'll
1) get the user to define the input data / row data / number of column (headers) / column data.
2) Then I'll transpose (is this the best way?) the old headers and make them become rows.
3) Stack all the values
4) Do a count of the number of old header columns (e.g., how many total non-unique dates were there) and then copy each Customer ID that number of times and list these out as rows to align with the right column headers (which will now each be rows).
I started out writing the below. You'll see how bad I am at VBA quite quickly. Prepare yourself! My question is:
1) I tried to do step 4) above in my last line of code (I know it's not in the order as listed above) but I thought it would be an easier step but my count isn't working - can you help guide me?
2) What is the best way to copy and paste all the values? Should I use a transpose function?
I'd really appreciate any tips! Please don't feel you have to write the code for me. You guys are so good at this and I really want to learn.
Thank you so much!
Louis
Sub LG_Data_Converter()
'Part 1 - Definitions
Dim Input_Range As Range
Dim Output_Range As Range
Dim Row_ID As Range
Dim Record_Descriptor_1 As Range
Dim Record_Descriptor_2 As Range
Set Input_Range = Application.InputBox( _
"Make Selection", "Select Input Range", Type:=8, Default:=Selection.CurrentRegion.Address)
Set OutputRange = Application.InputBox(prompt:="Select Output Location", Type:=8, Default:=Selection.CurrentRegion.Address)
Set Row_ID = Application.InputBox("Make Selection", "Select Record Label (Row Titles)", Type:=8, _
Default:=Selection.CurrentRegion.Address)
Set Record_Descriptor_1 = Application.InputBox( _
"Make Selection", "Select Record Descriptor #1 (#1 Column Title)", Type:=8, Default:=Selection.CurrentRegion.Address)
Set Record_Descriptor_2 = Application.InputBox( _
"Make Selection", "Select Record Descriptor #2 (#2 Column Title", Type:=8, Default:=Selection.CurrentRegion.Address)
'Part 2 - Conversion Process
Dim Count As Long
Count = Cells(Range(Record_Descriptor_1)).End(xlToRight).Columns