+ Reply to Thread
Results 1 to 26 of 26

Insert Rows based on Cell Values

  1. #1
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Insert Rows based on Cell Values

    First post!

    This looks like the best of the excel forums, so here I am..

    I've been teaching myself Excel using scroogle, but thought it might be wise to request some help with these macros..

    Here's what I'm trying to do:
    Please Login or Register  to view this content.
    If someone could help me convert this to a macro, it would be much appreciated!
    Attached Files Attached Files
    Last edited by macrobatics; 01-22-2011 at 11:48 AM. Reason: EDITED POST, attached UPDATED WORKBOOK.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Welcome to the Forum. To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Thank you for the quick reply!

    I have created a sample workbook and attached it to the original post as suggested.
    Last edited by macrobatics; 01-22-2011 at 05:53 AM.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Can you tell me why you want to add rows?

  5. #5
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    I want to add rows to type in new artists/titles to a music collection database.
    This macro will help me add the titles on the rows I want them located.
    Last edited by macrobatics; 01-22-2011 at 07:42 AM.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    You don't need to add rows. Simply add the da to the bottom of the table, then when finished sort by Artist

  7. #7
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    I appreciate that suggestion, but I don't want them listed alphabetically.
    I want to decide for myself what the location of each title in the sheet should be.

    This sheet:"LIST" dictates the order the titles are displayed in for all other sheets in my actual workbook.
    It is basically a list of albums which I may or may not have.
    Any new titles would first be added to this sheet.
    Then, whenever I obtain an album, I copy the row containing that title to another sheet (where I add other info such as the physical condition of the album.

    Those other sheets are not relevant to this macro, but hopefully this helps explain why I need the macro to work this way..

    Thanks again!
    Last edited by macrobatics; 01-22-2011 at 07:27 AM.

  8. #8
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Attached new sample workbook with "AFTER" sheet to show desired results:
    In the below attached workbook, the requested macro should:

    Skip Rows 1,2,3,4, because $D = "Status","e","g","e".
    In Row 5, $D = "t", and $C = "3", so 3 rows should be inserted above Row 5.
    In Row 5, change $D = "e" and $C = "".
    Skip Row 6, because $D = "e".
    In Row 7, $D = "u", and $C = "2", so 2 rows should be inserted below Row 7.
    In Row 7, change $D = "e" and $C = "".
    Skip Rows 8,9,10, because $D = "g","s","e".
    Exit Macro at Row 11, because $D = "*"

    Copy down ALL formulas & validation to added rows from above.
    For all inserted rows, $C = "", and $D = "s".
    Attached Files Attached Files
    Last edited by macrobatics; 01-22-2011 at 11:49 AM.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Is this what you mean?

    Note that I have used the Table feature to make the data into a Table, then Excel automatically copies the formulas
    Last edited by royUK; 01-22-2011 at 08:17 AM.

  10. #10
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Most likely I just don't know what I'm doing, but that workbook looks to be exactly the same as the one I uploaded..
    no table.
    no formula.
    If I'm wrong (very likely) please tell me how to find/run the macro..

    P.S.
    Please see new workbook attached above your most recent post for desired results.
    Last edited by macrobatics; 01-22-2011 at 08:15 AM.

  11. #11
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Sorry I've attached the wrong workbook
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Okay, I figured out how to make it a table. (select cell, ctrl-t)

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Or from the Insert Tab

  14. #14
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    It works at least partially.
    The original I uploaded had an error. The value "2" should be located at C7 instead of C9.
    When running the macro with that value in the wrong cell, the rows were inserted in the wrong place.

    After moving the "2" from C9 to C7, I created the table.
    Then I ran the macro, and the rows were inserted in the right places!
    However, the formulas were not copied down to the new rows, and the C & D values still need to be changed after adding the rows.

    Thanks!
    Last edited by macrobatics; 01-22-2011 at 09:31 AM.

  15. #15
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Last edited by macrobatics; 01-22-2011 at 11:51 AM.

  16. #16
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Last edited by macrobatics; 01-22-2011 at 11:09 AM.

  17. #17
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    In my Excel 2003 workbook the formulas copied down OK. Try this amended code, note I've already created the table from the Inert Tab, keep it as a Table for enhanced features
    Attached Files Attached Files
    Last edited by royUK; 01-22-2011 at 11:43 AM.

  18. #18
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Exclamation Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    No change in results, and I see no table upon opening the workbook.
    Creating the table myself before running the macro seems to make no difference either.
    Formulas were not copied down.

    Also, creating the table changes $I1 from "1" to "12" and I can't change it back while table is in place.

    The macro works for inserting rows when $C>"1", but often does not work correctly otherwise.

    Please Login or Register  to view this content.
    Please use this UPDATED WORKBOOK to test your macro:
    Attached Files Attached Files

  19. #19
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    I've checked the file & it works for me. I can't understand your code above

  20. #20
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Quote Originally Posted by royUK View Post
    I've checked the file & it works for me. I can't understand your code above
    Please ignore that post then.
    It's meant to be the same as the original.
    I just thought it might be easier to understand this way.

    The last macro you uploaded seems to work when $C > "0".
    What I need changed is when $C = "0" (or is blank),
    it should ALWAYS insert ONE ROW in EXACTLY THE SAME position it would if $C = "1".

    [$C = "1", $C = "0", $C is blank] All 3 should have the EXACT same result.

    With the current macro, this is not true.

    Please add your macro to this workbook to see what I mean:
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Question Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Quote Originally Posted by royUK View Post
    I've checked the file & it works for me. I can't understand your code above
    Nothings changed. I'm still trying to do the same thing as in my original post. I was just trying to word it differently to see if that would help.

    The macro is still far from functioning correctly. It is quite easy to see if you play around with the workbook I uploaded. Run your macro on my most recent upload and many of the rows are not inserted at all.


    Is it possible you did not upload the correct macro?

  22. #22
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Well apart from the last Column the new table looks the same as your after example. I can't inderstand why you have a formula based on a column header, if I change I2 to =Row(a2) then the result seems to be what you want

  23. #23
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    Your macro works for your workbook example, but here's an instance where it fails:
    (on your uploaded workbook/macro)
    CHANGE:
    C2:C10
    TO:
    (blank)
    2
    (blank)
    1
    (blank)
    (blank)
    (blank)
    1
    (blank)

    CHANGE:
    D2:D10
    TO:

    g
    t
    g
    t
    g
    u
    g
    u
    g

    Then run the macro.

    Among other things, sometimes it inserts the rows above when they should be below.

    *I figured out this is because my repeated requests for the macro to handle zeroes or blank values in Column D were completely ignored/never addressed.
    Last edited by macrobatics; 01-23-2011 at 12:44 AM.

  24. #24
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Question Almost there!

    Okay, I'm almost to the finish line with this.. I've been playing with the macro royUK gave me, and it's pretty close to what I need now.

    There's just a few (three) small changes I would like to make:

    1. I would like the macro to treat blank cells in column C as if they contain the numeral "1". (so exactly one line is inserted). Is there any way to do this?
    (I've highlighted that part of the code in bold text.)

    After The Rows Are Inserted & Column A Is AutoFilled:
    2. I would like just the values from Column A pasted to Column B.

    3. I would like any values of "t" or "u" in Column D changed to a value of "e".

    Please Login or Register  to view this content.
    Any help would be much appreciated!

    Thanks..
    Last edited by macrobatics; 01-23-2011 at 12:07 AM.

  25. #25
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert (x) number of Rows (above or below) based on Cell Values in two Columns

    [QUOTE
    *I figured out this is because my repeated requests for the macro to handle zeroes or blank values in Column D were completely ignored/never addressed.[/QUOTE]

    I was addressing the initial request to sort that out first. I'm not actually on your payroll!

  26. #26
    Registered User
    Join Date
    01-22-2011
    Location
    Chicago, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    19

    Post To royUK..

    My apologies for taking that little jab at you there. It would be silly of me to think you were trying to be anything but helpful. When I wrote that I was frustrated because I realized that if you had told me which part of my request you had not addressed, I would not have kept disagreeing with you about it or spent hours trying to figure out why the macro did not behave as I expected.

    Now that I've spent some time tinkering with the VBA you supplied, I have come to understand and appreciate it a whole lot more.
    I now realize my "dummy workbook" could have been much better, and I'm sure it didn't help that I kept going back and editing my posts.

    Please forgive/accept my apology, and allow me to thank you for your effort and time spent helping me! Your macro is very good. I have learned from it, and it has gotten me very close to the results I desire.

    Thank you Roy!!! I do hope we'll be friends.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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