Hi – I’m really hoping someone can help me with some code to display data in the format I’m looking for. I’m still learning about vba code and I’ve been stuck trying to find a solution, as the ranges in my data will change from week to week.
I have attached my data sample here. I have a range of values in column H that will change in my future data samples, so the code has to be based on the number of unique values in this dynamic range. The values in column A are also unique, they are listed vertically to correspond with each unique value in column H.
MY DESIRED GOAL: I am trying to have the vertical data in columns A, H-J output horizontally based on my dynamic range in row H. I used a formula to calculate the number of unique values in column H, but I still need to figure out how to make the formula dynamic (the formula is located in cell L1).
I created an example of what I want the data to look like in the end, you can see this to the right of column L. I have a grid where the unique values from column H are listed across the top starting in cell O1. Underneath that, there are two horizontal rows for each value in column A, one row is hard-coded titled “weekend” and the second row is hard-coded titled “weekday”. I used two different colors in attempt to better illustrate. The data from columns I-J should output horizontally in this grid, starting in cell O2 in my example. Once the first two rows of data have been populated in the grid, the code needs to know to move down to begin the next loop (i.e. move to O4-O5 to work on the data for the next unique value in column A, which starts in row 22 in my sample). This needs to continue until all rows (i.e. in my sample there are 1121 rows of data, but the number of rows will be dynamic in future data) have been outputted into the grid. The biggest problem I have is that this data sample will change from week to week, the only thing constant is that I know what type of data will be in each column, but the number of unique values in column H will always change.
Note: it doesn’t matter to me if the final data is outputted on the same tab like I have it now, or if it’s on a new tab.
CODE: I don’t have much code at this point since I’m not quite sure from what angle to come at this with, I just have a few very basic samples that I have come up with so far. Here is some static code for getting the values for O2-O3 based on the values in I2 and J2; this is just a model to work off of to build out the grid I need, again I need to figure out how to make it dynamic:
'if it's a weekday event
If Range("I2") = "No" Then
Range("O2").Value = "No"
End If
If Range("I2") = "No" And Range("J2") = "Yes" Then
Range("O3").Value = "Yes"
End If
If Range("I2") = "No" And Range("J2") = "No" Then
Range("O3").Value = "No"
End If
'if it's a weekend event
If Range("I2") = "Yes" Then
Range("O3").Value = "No"
End If
If Range("I2") = "Yes" And Range("J2") = "Yes" Then
Range("O2").Value = "Yes"
End If
If Range("I2") = "Yes" And Range("J2") = "No" Then
Range("O2").Value = "No"
End If
Display More
I also have some code to transpose the number of unique values listed in column H to list them horizontally starting in O1, but my formula in L1 somehow needs to be modified to be dynamic.
Dim iNumEvents As Integer
iNumEvents = Range("L1") + 1
Range(Cells(2, 8), Cells(iNumEvents, 8)).Select
Selection.Copy
Range("O1").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
Range("O1").Select
Any help would be truly appreciated. This is my first time posting so please forgive anything I may have overlooked.