+ Reply to Thread
Results 1 to 23 of 23

Insert Rows Based on Cell Value VBA

  1. #1
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Question Insert Rows Based on Cell Value VBA

    Hi all,

    I am looking to write some VBA code that will look for a numerical value in column "O" and insert that amount of rows but minus 1. (So if 3 appears, it will insert 2 rows).

    I also need to then copy the information from the rest of the row and paste the values. All the cells contain formula, so I just need to values to be pasted here.

    I am completely new to VBA, but quite an advanced Excel user.

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    Insert rows Where? Under the value in O, over the value of O, at the top of the page?
    Copy from "the rest of the row" - Column A through LastColumn minus Column O, or the cells after column O going to, or something else entirely?

  3. #3
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    Hi Jomili,

    Insert the required number of cells under the target row.

    The whole row is fine to be copied, I just wrote it a bizarre way!

  4. #4
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    See if this does the trick. In the example we're searching for the value of 131 in column O. I tried to comment the code but feel free to ask if you don't understand something.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    I've tried the code but it doesn't seem to work for me.

    I have attached the workbook I am working on. In the third sheet 'GRP' it is pulling information through and calculating that a panel would need to be cut a set number of times (column O) from a sheet. I am trying to get it add the rows below based on this value, so in the case of the first row, it would be two extra rows, and copy the data down. Does that make sense?
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    Yes it does. Now that I have an example to work with I should have something for you pretty quickly.

  7. #7
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Insert Rows Based on Cell Value VBA

    Hi ashpic1483,
    I also went off in a bit of a false direction, but I leave it now with jomili has he is doing amazing work for you.
    If you get a chance, can you take a quick look here:
    http://www.excelforum.com/showthread...31#post4494031

    Thanks
    Alan
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  8. #8
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    Cheers pal.

    I'd love to learn how to do VBA, I just don't know where or how to start!

    Looking forward to seeing what magic you can perform.


    A

  9. #9
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    I don't know about "amazing" but I think I might have a solution for you. The Attachment contains 2 GRP sheets, one before, one after the code. If the result is the way you want it, the code in the workbook should do it for you.
    If you have tons of rows let me know and I can incorporate some tweaks to speed it up for you.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    Whoops, just noticed you had some formulas in there, that in the new rows were copied as values. If you want to keep those as formulas, in this section of the code change ".Value" to ".FormulaR1C1". I haven't tested it but it should work just fine for you.
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    Quote Originally Posted by jomili View Post
    Whoops, just noticed you had some formulas in there, that in the new rows were copied as values. If you want to keep those as formulas, in this section of the code change ".Value" to ".FormulaR1C1". I haven't tested it but it should work just fine for you.
    Please Login or Register  to view this content.
    This is absolutely perfect!

    Copying values is exactly what I needed it do so that it stays the same as the row above.

    Genius! Proper genius!

    Thank you buddy!

  12. #12
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    I don't think I've ever been called a "Proper genius" before; smartypants, knowitall, jerk, yes, but not proper genius. Thanks!

  13. #13
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    Quote Originally Posted by jomili View Post
    I don't think I've ever been called a "Proper genius" before; smartypants, knowitall, jerk, yes, but not proper genius. Thanks!
    Hi Jomili,

    I've been running the code that you wrote and I have noticed that after running the macro (even using the test data in the file I sent you) it moves a lot of the data around. Do you know if there is a way to keep it in the same order, but insert the rows below?

  14. #14
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    Not sure what you mean by "moving the data around". The column order seems the same, the only change I see is that, for instance, the first line (Row 2), with a value of 3 in the Qty To Fill column, occupies 3 lines afterward (Rows 2-4). Because of the insertion of the 2 additional lines, the next data set, with a 6 in the Qty to Fill column, begins on Row 5 AFTER the macro, but on Row 3 BEFORE the macro. Is it the row numbers that you're talking about changing? If so, how do you want the result to appear?

  15. #15
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    Quote Originally Posted by jomili View Post
    Not sure what you mean by "moving the data around". The column order seems the same, the only change I see is that, for instance, the first line (Row 2), with a value of 3 in the Qty To Fill column, occupies 3 lines afterward (Rows 2-4). Because of the insertion of the 2 additional lines, the next data set, with a 6 in the Qty to Fill column, begins on Row 5 AFTER the macro, but on Row 3 BEFORE the macro. Is it the row numbers that you're talking about changing? If so, how do you want the result to appear?
    Sorry Jomili, I don't think I explained it very well.

    I have attached a couple of screenshots to show you what the data looks like before running the macro, and afterwards. I hope it makes more sense that my random vague explanation.
    Attached Files Attached Files

  16. #16
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    I'm not sure why that's happening. In the sample I attached earlier we DON'T get a result like that: did you make any changes to the macro, or to the sample data? 'd need to see what data you're running the macro on that's causing a result like what you're seeing. Can you provide another sample?

  17. #17
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    Quote Originally Posted by jomili View Post
    I'm not sure why that's happening. In the sample I attached earlier we DON'T get a result like that: did you make any changes to the macro, or to the sample data? 'd need to see what data you're running the macro on that's causing a result like what you're seeing. Can you provide another sample?
    Hi,

    I have attached the sample data that was shown in the screen shots.

    Hope it helps.
    Ash
    Attached Files Attached Files

  18. #18
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    I see the problem(s); working on it now.

  19. #19
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    Okay, I think we've got it done here. This also might be easier code to follow. I've tried to comment all the steps. Let me know if that gets it or more tweaking is needed.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  20. #20
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    It works like a dream!

    Thank you!

  21. #21
    Registered User
    Join Date
    06-07-2013
    Location
    Colchester, England
    MS-Off Ver
    Excel 2010
    Posts
    10

    Re: Insert Rows Based on Cell Value VBA

    Is there a way to make the macro run on a specific sheet, even if it is not the active sheet?

  22. #22
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    Sure thing, all we need to do is activate whichever sheet you want. In the code, where it says "With Activesheet", change it like shown below:
    Please Login or Register  to view this content.
    That will dump you out on the Grp (2) sheet when you're all done. If you want to return to the sheet you were originally on when you started the macro you'd need to capture that sheet name before you Activate the sheet you want to run the macro on, then go back to it at the end. See below.
    Please Login or Register  to view this content.

  23. #23
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Insert Rows Based on Cell Value VBA

    I had a few range references coded wrong, which was why I Activated the sheet we worked on in the last code. I've corrected it now, so there's no need to activate any sheet using the code below:
    Please Login or Register  to view this content.

+ 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 rows based on the current cell value and, then, copy some cells into the new rows
    By Excel-RZ in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-03-2013, 05:19 PM
  2. [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
  3. Insert rows based on a value in a cell and copy the data down into the new rows
    By eyeman96 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2013, 07:54 AM
  4. [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
  5. Insert rows based on cell value
    By johncena in forum Excel General
    Replies: 3
    Last Post: 02-28-2010, 11:30 AM
  6. Insert rows based on a value in a cell
    By the_dusan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2009, 05:20 PM
  7. Delete Rows based on cell value and then Insert rows alternatively with a formula
    By acsishere in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-11-2008, 04:27 PM

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