+ Reply to Thread
Results 1 to 15 of 15

Insert Number of Rows Based On Number of Populated Cells and Transpose Data

  1. #1
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Hi everyone,

    I have a sheet containing a date in column A, a description in column B, and a varying number of reference codes in columns D to AZ.

    What I'm trying to achieve is to count the number of codes in (for example) cells D2 to AZ2 (ignoring empty cells), insert the equivalent number of entire rows into the table immediately following the row containing the data, copy the codes (again ignoring the blanks) and transpose the data, pasting it into column C. After doing this, I want to copy a single cell (O1 in the attached example) and paste it into column D an equivalent number of times to the number of codes which were just pasted.

    Then repeat down the sheet.

    Hopefully the attached example helps.

    Thanks in advance to anyone who can help me with this....

    Andy
    Attached Files Attached Files

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,728

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    You could do it with a few formulae, but as you have posted in the programming forum presumably you are looking for a macro to do it for you, so I'll leave that for someone else.

    Pete

  3. #3
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Hi Pete,

    If you have any tips on doing this with formulae, I'd be happy to hear them!

    Andy

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,885

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    If this presentation is acceptable, then this can be completed with a couple of steps in Power Query

    Data Range
    A
    B
    C
    D
    1
    Column1
    Column2
    Column3
    Column4
    2
    1/1/2019
    2/1/2019
    3/1/2019
    4/1/2019
    3
    Test 1
    Test 2
    Test 3
    Test 4
    4
    5
    42
    353
    5363
    64
    6
    2452
    356
    6
    46746
    7
    45
    356
    467
    65
    8
    525
    467
    647
    9
    245
    467
    10
    245
    11
    2454
    12
    245
    13
    245
    14
    245

    Here is the Mcode

    Please Login or Register  to view this content.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Hi Alan,

    I need to end up with all of the reference codes in one column.

    Andy

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    VBA
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Thank you +jindon, that works great!!

    I have some questions though:

    1. I want to move the word 'Apples' into cell C2 on a separate sheet called 'XDATA' within the same workbook. How can I do this?

    2. When I am using the VBA with different workbooks in the future, the number of columns containing the codes will vary. Can I specify a number in cell 'XDATA' cell D2 which will allow me to change the total number of columns containing the codes?

    3. When I am using the VBA with different workbooks, the number of columns at the beginning of the sheet containing data such as 'Date', 'Description', etc, will vary. Can I specify a value for this in 'XDATA' cell E2?


    Andy

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Not clear to me,

    If you upload a workbook the the data and all the conditions in 'XDATA', it will help.

  9. #9
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    I have attached an example, plus I have added more features in XDATA to improve the flexibility of the project.
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    OK, thanks for the clear explanation.

    Delete row(1) from "Source Data 123" sheet and run the code.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Thanks, that works great in your example.

    However, once I had added my own data and increased the number of columns, i get a 'Subscript out of range' error at the following line:

    b(n, ii) = a(i, ii)

    Also, I have added some more fields to the XDATA sheet. Sorry to keep making changes.

    Please see the attached example.

    Andy
    Attached Files Attached Files

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Are you sure that you always have blank(s) in the header row?

  13. #13
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Yes, there are often some entirely empty columns in our sheets - but if that is a problem we could add some data into the header row here ("EMPTY", for example).

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    The result is obviously different from yours...
    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    09-28-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    141

    Re: Insert Number of Rows Based On Number of Populated Cells and Transpose Data

    Thank you, that's absolutely perfect. I really appreciate your help......

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Insert number of rows with data to new sheets based on values in cells
    By keithtran12 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-28-2013, 11:57 AM
  2. Replies: 1
    Last Post: 05-30-2013, 06:29 PM
  3. [SOLVED] insert rows based on number in cell and copy the data down into the new rows
    By pziefle in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-05-2013, 11:19 AM
  4. Replies: 6
    Last Post: 10-04-2012, 11:12 AM
  5. [SOLVED] Insert Multiple Rows Based Off Number in Cell and Copy Data From Above New Rows
    By tstell1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2012, 04:15 PM
  6. Replies: 9
    Last Post: 06-08-2012, 06:22 PM
  7. Replies: 0
    Last Post: 05-18-2012, 11:18 PM
  8. insert text in columns based on maximum number of rows containg data
    By twrizzo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-30-2007, 03:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1