Hey everyone,
Below message is split into 3 parts.
1) Context of request
As many of the above generous individuals already know, I am trying to learn the concept of arrays / other vba by carrying out a practical task to convert data in matrix format to list format, in a dynamic, scalable way (e.g., the code can adjust to accommodate varying numbers of row headers and number of data rows / columns).
Here is an example of what I would want to do (convert A > B in the below).
A
Metric |
Bookings |
Bookings |
4 Wall |
4 Wall |
Date |
7/1/2014 |
8/1/2014 |
11/1/2014 |
12/1/2014 |
1052 |
$ 38K |
$ 31K |
$ 120K |
$ 29K |
1105 |
$ - |
$ - |
$ 9K |
$ 0K |
1110 |
$ 25K |
$ 56K |
$ 37K |
$ 35K |
1123 |
$ - |
$ 25K |
$ - |
$ 21K |
B
Customer ID |
Metric |
Date |
Value |
1052 |
Bookings |
7/1/2014 |
$ 38K |
1052 |
Bookings |
7/1/2014 |
$ - |
1052 |
Bookings |
7/1/2014 |
$ 25K |
1052 |
Bookings |
7/1/2014 |
$ - |
1105 |
Bookings |
8/1/2014 |
$ 31K |
1105 |
Bookings |
8/1/2014 |
$ - |
1105 |
Bookings |
8/1/2014 |
$ 56K |
1105 |
Bookings |
8/1/2014 |
$ 25K |
1110 |
4 Wall |
11/1/2014 |
$ 120K |
1110 |
4 Wall |
11/1/2014 |
$ 9K |
1110 |
4 Wall |
11/1/2014 |
$ 37K |
1110 |
4 Wall |
11/1/2014 |
$ - |
1123 |
4 Wall |
12/1/2014 |
$ 29K |
1123 |
4 Wall |
12/1/2014 |
$ 0K |
1123 |
4 Wall |
12/1/2014 |
$ 35K |
1123 |
4 Wall |
12/1/2014 |
$ 21K |
2) My Request
Two steps of the process are, I think, to 1) define / find the data set; 2) convert all of the values from format A > B. You have been awesome in helping me understand how to do this and, with your help (esp. Pascal, Mark, jolivanes, snb). You all helped me come up with the below code.
Now my question is - how do I also convert the rows metric / date / customer ID (examples only that apply to this case - could change depending on dataset) in an efficient, scalable way? If possible, I would love to get guidance on the concepts behind this and how to do it, rather than the exact code I need. If you can possibly give me a high level description of the / a recommended methodology, then I'd love to be able to use that to come up with an "answer" myself that I can hopefully then check with you all.
3) My Code up till now (for reference)
Sub LG_Data_Converter_2()
'Part 1 - Convert_Data
Dim Nmbr_Headers As Byte
Nmbr_Headers = 2
'Nmbr_Headers = Application.InputBox("Input Required", "How many Header Rows are there?", Type:=1, Default:=2)
Dim FirstRow As Long, LastRow As Long, FirstColumn As Long, LastColumn As Long
FirstRow = Cells.Find("*", , , , xlByRows, xlNext).Row
LastRow = Cells.Find("*", , , , xlByRows, xlPrevious).Row
FirstColumn = Cells.Find("*", , , , xlByColumns, xlNext).Column
LastColumn = Cells.Find("*", , , , xlByColumns, xlPrevious).Column
Dim No_Data_Rows As Long
No_Data_Rows = LastRow - FirstRow - Nmbr_Headers + 1
Dim No_Data_Columns As Long
No_Data_Columns = LastColumn - FirstColumn + 1 - 1 '(Take into account the customer ID column)'
Set DatasetRange = Range(Cells(FirstRow + 1, FirstColumn + 1), Cells(LastRow, LastColumn)) 'all data incl headers, i've been playing around wiht this, can obviously adjust it to be just the values only and not the headers
Dim Dataset() As Variant
ReDim Dataset(1 To DatasetRange.Cells.Count, 1 To No_Data_Columns)
i = 1
j = 1
For Each Column In DatasetRange.Columns
For Each cell In Column.Cells
Dataset(i, 1) = cell
i = i + 1
Next cell
Next Column
Cells(LastRow + 3, LastColumn + 1).Resize(UBound(Dataset)).Value = Dataset 'write the array to the sheet.
'These are random notes - what I've been trying hasn't been working too well so I'm coming back to you all
'Part 2 - Convert Header Metric
'Dim Metric_Row As Range, Metric As Variant, No_Metrics As Long, List As Object
'Set Metric_Row = Range(Cells(FirstRow, FirstColumn + 1), Cells(FirstRow, LastColumn))
'
'Set List = CreateObject("Scripting.Dictionary")
'
'For Each Metric In Metric_Row
'If List.Exists(Metric.Value) = False Then List.Add Metric.Value, Nothing
'Next
'
'No_Metrics = List.Count
'
End Sub