+ Reply to Thread
Results 1 to 15 of 15

Insert rows by macro?

  1. #1
    Registered User
    Join Date
    01-15-2010
    Location
    Cymru
    MS-Off Ver
    Excel 2003
    Posts
    14

    Insert rows by macro?

    Hi,

    I've created a macro to assign to a button that inserts a row above say row 10, then another macro to assign to a different button that inserts a row above row 20. My problem is, having clicked the first button a row is inserted above row 10 but of course row 20 now becomes row 21, so now I need the second button to insert a row above row 21 instead of 20.
    Is there any way I can write a macro that remembers the old row numbers?
    Hope I haven't confused you all as much as I've confused myself
    PS I'm not familiar with Visual Basic, I just use Excel's 'record a macro' function.

    Thanks for listening.
    Last edited by celfyn; 01-21-2010 at 12:03 PM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,445

    Re: Insert rows by macro?

    Bore Da Celfyn.

    Is there a feature in your spreadsheet that you're trying to insert the row above?
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Insert rows by macro?

    Hi,

    Rather than inserting rows starting at the top, change the macro to insert the highest row number first. i.e. First add a row above row 20, then above row 10.

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Insert rows by macro?

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Rather than inserting rows starting at the top, change the macro to insert the highest row number first. i.e. First add a row above row 20, then above row 10.

    HTH
    Sorry, forget that advice, I'd missed the point about this being two macros.

    Why not just keep a cell variable in the workbook which is incremented every time you run macro 1. Then when you run macro 2 use that variable to work out which row should be inserted.

    HTH

  5. #5
    Registered User
    Join Date
    01-15-2010
    Location
    Cymru
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Insert rows by macro?

    Quote Originally Posted by Richard Buttrey View Post

    Why not just keep a cell variable in the workbook which is incremented every time you run macro 1. Then when you run macro 2 use that variable to work out which row should be inserted.

    HTH
    Richard,

    That sounds exactly like what I need, is it something you could explain here or is it more involved than that.
    Sorry As you know dout can tell, I'm no Excel expert.

    Sweep,

    Bore da i chi hefyd, very perseptive of you to notice. Doing some missionary work in England are you? (sorry for off topic)
    Yes, I've got different sections that my end users may need to add rows to without affecting the other sections. Might not be the best way to go about things, but as I say, I'm no excel expert.

  6. #6
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,445

    Re: Insert rows by macro?

    If you could post an example, it will help all with helping you! It will be possible to identify the point at which to insert the row.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Insert rows by macro?

    Hi,

    As Sweep has said it might be easier if you upload a workbook. However in essence you need to name a cell somewhere that will hold the variable, call it say 'RowNo'.

    Then as the last line of Macro1 add:

    Please Login or Register  to view this content.
    Then in the second macro use some code like:

    Please Login or Register  to view this content.
    You may want to use another variable if row 20 (which is hard coded in the above) changes, and use that variable instead of 20.

    HTH

  8. #8
    Registered User
    Join Date
    01-15-2010
    Location
    Cymru
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Insert rows by macro?

    Hi guys,

    Sorry to take so long to respond, couldn't connect yesterday for some reason.
    I can't attach the actual workbook but I've tried to recreate it in a simpler format for you.

    When I've filled up all the rows in section 1, I'd like to add a row in that section every time I click 'add row 1', then the same for section 2 and so on.

    Sorry to be a pain but I really do appreciate all your help.

    Thanks again

    Celfyn.
    Attached Files Attached Files

  9. #9
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,445

    Re: Insert rows by macro?

    How about looking for the next title, and inserting a row there?

    Please Login or Register  to view this content.

  10. #10
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Insert rows by macro?

    Hi,

    Why not just use a single macro. e.g.
    Please Login or Register  to view this content.
    This will insert a new row immediately above the next section title below the active cell when you run the macro.

    Rgds

  11. #11
    Registered User
    Join Date
    01-15-2010
    Location
    Cymru
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Insert rows by macro?

    Diolch Dave,
    Thanks Richard,

    I'll try to get my head around those and give it a go!

  12. #12
    Registered User
    Join Date
    01-15-2010
    Location
    Cymru
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Insert rows by macro?

    Dave,

    I've tried your suggestion and it works great, thanks, but is there a way to add to that code so that the formula in columns D and E, in the row above the one just inserted, can be dragged into columns D and E of the newly inserted row?

  13. #13
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,445

    Re: Insert rows by macro?

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    01-15-2010
    Location
    Cymru
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Insert rows by macro?

    Dave,

    Diolch yn fawr iawn, Mae'n gweithio, dwi ddim yn deall, ond mea'n gweithio!!

    Celfyn.

  15. #15
    Registered User
    Join Date
    01-15-2010
    Location
    Cymru
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Insert rows by macro?

    I'm really sorry to be a PITA but how do I extend the range in cell E1 in the attached, so that it includes the newly inserted rows?
    I have tried to work it out for myself but I'm not getting anywhere fast.

    Thanks in advance

    Celfyn
    Attached Files Attached Files

+ 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