vba code to transpose dynamic ranges of data

  • 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:




    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.


    Code
    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.

  • Re: vba code to transpose dynamic ranges of data


    try

  • Re: vba code to transpose dynamic ranges of data


    Hi jindon - wow, thanks for your help! So far I do see something that isn't working correctly though. It seems as though anything with a "yes" in column "I" isn't outputting in the grid properly.


    Example 1: see the first discount_key from column A ("C151P2050B5378R"). After running the code - in the output grid see performance_code "EUC1118E" - the weekday & weekend output BOTH say "yes". There should never be a time when both the weekend and weekday output rows say "yes" at the same time since that's contradictory. In this specific case, based on the values in columns I-J, the grid should say: weekend "yes" / weekday "no"


    Example 2: see the discount_key "G3D0116". After running the code - in the output grid see performance_code "EUC1118E" - here both weekend and weekday say "yes". In this specific case, based on the values in columns I-J, the grid should say: wekeend "no" / weekday "no"


    Are you able to help modify the code to fix this? Thanks again.

  • Re: vba code to transpose dynamic ranges of data


    I thought ColumnJ(Applied) for weekday.


    Quote


    based on the values in columns I-J


    Can you explain it with logically understandable format?

  • Re: vba code to transpose dynamic ranges of data


    Column I tells us if the performance_code is on a weekend or a weekday.


    Column J tells us if the discount_key applies to that performance_code.


    Here is some static code from my original post to help explain; I added verbal descriptions for each possible scenario:



    Sorryif that wasn't clear, does it make sense now?

  • Re: vba code to transpose dynamic ranges of data


    If J = No, both No
    If J = Yes and I = Yes, Yes in weekend, No in weekday
    If J = No and I = Yes, No in weekend, Yes in weekday


    Is this correct?

  • Re: vba code to transpose dynamic ranges of data


    Almost. See below with correction:


    If J = No, both No
    If J = Yes and I = Yes, Yes in weekend, No in weekday
    If J = Yes and I = No, No in weekend, Yes in weekday

  • Re: vba code to transpose dynamic ranges of data


    Yeah, of course.


    Check this

  • Re: vba code to transpose dynamic ranges of data


    Jindon – thank you SO much! I can’t tell you how thankful I am for your quick and brilliant help!

    I am going through and checking the output for accuracy, so far everything looks great and I haven’t seen any issues.

    I’m not sure if you/other posters in the forum ever explain what a code actually is doing – I am just curious if you could share any insight on the code itself. I haven’t seen anything like that before and don’t fully understand it. I am 100% happy as long as it works, it would just be nice to know more about it. BUT if you don’t normally do that really don’t worry about it, I won’t expect a response on this question. Either way, THANK YOU!!!

  • Re: vba code to transpose dynamic ranges of data


    I'm not good at doing this....
    You can step through with LocalWindow open,
    so that you can see what is going on within array

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!