I have a dataset in three columns composed of a repeating set of UUIDs in the first column, string responses for each UUID in the second, and a code for each response in the third. I need to break this out into multiple sets of columns, one for each repeating set of UUIDs. See the below illustration:
I Have:
UUID RESPONSE Resp. Code
id1 String1 Code1
id2 String2 Code7
id3 String3 Code3
id1 String4 Code3
id2 String5 Code5
id3 String6 Code1
I need:
UUID RESPONSE Resp. Code RESPONSE Resp. Code
id1 String1 Code1 String4 Code3
id2 String2 Code7 String5 Code5
id3 String3 Code3 String6 Code1
Note that while there are 3 UUIDs illustrated here, I'm actually dealing with 1377.
I've attempted to write a macro for this operation (pasted below), but I am a complete noob to VBA and Excel macros, so it is hacky and does not do even close what I want it to.
Sub DestackColumns()
Dim rng As Range
Dim iCell As Integer
Dim lastCol As Integer
Dim iCol As Integer
Set rng = ActiveCell.CurrentRegion
lastCol = rng.Rows(1).Columns.Count
For iCell = 3 To rng.Rows.Count Step 3
Range(Cells(1, iCell), Cells(2, iCell)).Cut
ActiveSheet.Paste Destination:=Cells(lastCol, 1)
Next iCell
End Sub
All help appreciated!
One take on a VBA code that will achieve this is:
This code will place the new data structure on a new sheet (i.e. not overwrite your original data) and with this code you do not need to worry about if the data is sorted properly.
Here is a somewhat different approach. I have set up a user defined class called cUUID. The class has the properties of the UUID, Response, ResponseCode and a Collection consisting of the paired Response and ResponseCode.
We create a Collection of this class object, where each member of the collection is a specific UUID (since that's how you want to group them).
The code iterates through your data source, creating these objects "on the fly". We then create an array containing all the results, and write this array to a different worksheet.
It should be obvious in the code how to change those worksheet names, and, if necessary, the locations of the source data and results.
After you Insert the Class Module, you must select it,
F4
and rename it cUUIDClass Module
Regular Module
The UUID's will be sorted alphabetically. The code should work with varying numbers of UUID's, and varying numbers of responses to each of the UUID's.
Your sample code indicates that you wish to remove the original values in favor of the new matrix. To that end, I would suggest running this on a copy of the data first.
I haven't populated the headers into the new columns but that should be a minor manual operation.