+ Reply to Thread
Results 1 to 5 of 5

Copy row(s) based on cell value

  1. #1
    Registered User
    Join Date
    04-11-2017
    Location
    Scotland
    MS-Off Ver
    2010
    Posts
    5

    Copy row(s) based on cell value

    Hi all,

    I'm trying (with little success being very new to this!) to write a VBA code to copy a row and paste it a number of times below based on the value in a cell within the same row. Columns A-T are populated with a checklist and column U tallies the entries to give a numerical number, and the idea is that each checked Column should have its own row. An example would be....

    1| X X X X 4 // This is what the spreadsheet currently looks like, where "4" is in column U


    1| X X X X 4 // This is what I am hoping to achieve
    2| X X X X 4
    3| X X X X 4
    4| X X X X 4

    My code is as follows:


    Please Login or Register  to view this content.
    The code successfully steps through each row in column U and identifies which has to be copied, copies the row and pastes into a new row below - the issue I have is that after the outer For Next loop the Active Cell moves back to Row 2, when I need it to skip over the newly inserted rows and look at row 5. I have tried to offset the active cell within the internal loop but the final Next seems to overwrite this back to row 5.....

    Hopefully I've explained my issue well enough - any help would be appreciated!

    (I think the easiest solution might be to insert ABOVE the copied row, but offsetting it seems to go up 2 rows, any tips?!)

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Copy row(s) based on cell value

    To delete or insert rows you need to step backward through your range, otherwise it gets very tricky (- row numbers that VBA has not yet dealt with keep changing).
    Your last row is:
    Please Login or Register  to view this content.
    So your loop is:
    Please Login or Register  to view this content.
    Last edited by kev_; 04-11-2017 at 10:51 AM.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Copy row(s) based on cell value

    No need for a loop. Change a below to the column that will have the last row in worksheet

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Copy row(s) based on cell value

    Can you upload an example workbook?

  5. #5
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Copy row(s) based on cell value

    Hi Andrew- As mike7952 mentioned, a actual workbook would have simplified things, but this might work for you.
    Please Login or Register  to view this content.
    Good luck! If this answers your needs, please mark the thread as SOLVED ('Thread Tools' at top)...and maybe click on the Add Reputation star at the bottom of this post. That's the best way to thank everyone who helped.
    Last edited by leelnich; 04-11-2017 at 12:41 PM.

+ 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] Copy Single Cell Value to Separate Worksheet Based on Value of Another Cell
    By lday75 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2017, 11:49 AM
  2. Copy and Paste Cell X number of times based on Adjacent Cell
    By GabeGabe in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-07-2016, 11:40 AM
  3. Replies: 6
    Last Post: 11-19-2015, 09:36 PM
  4. Replies: 6
    Last Post: 12-08-2014, 01:28 AM
  5. [SOLVED] copy cell values in one cell based on value of adjacent cells
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-20-2014, 08:48 AM
  6. Copy Cell to next empty cell on other worksheets based on date and category
    By mainebf in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-04-2012, 02:43 PM
  7. Replies: 3
    Last Post: 07-15-2010, 08:49 AM

Tags for this Thread

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