+ Reply to Thread
Results 1 to 7 of 7

Macro for copying & inserting a variable number of rows into a separate workbook

  1. #1
    Forum Contributor
    Join Date
    01-11-2011
    Location
    Frederick, MD, USA
    MS-Off Ver
    Excel 2007
    Posts
    125

    Macro for copying & inserting a variable number of rows into a separate workbook

    I've got a workbook that I use for archiving data generated in another series of worksheets. I'd like to have a simple "Copy to archive" button that copies all of the non-empty cells in that worksheet (always a set number of columns, but varying number of rows) to the first empty row in the archive workbook. Is this possible, or would I have to settle for some ability to insert at a given point in the archive workbook, pushing the previously-entered rows down?

    I've attached two worksheets as examples of what I'm wanting....the Origin and Archive worksheets. I'd like a button on Origin that copies all of the non-empty cells in A3:F200 to the first empty row in Archive; in this case, that would be copying A2:F5 in Origin to A6:F9 in Archive (Hope that makes sense.)

    Archive.xlsm
    Origin.xlsm

    Bonus style points if the macro is commented such that a macro newbie like myself can understand it.
    Last edited by Gunther Maplethorpe; 10-28-2013 at 05:34 PM.

  2. #2
    Forum Contributor
    Join Date
    10-12-2012
    Location
    Bournemouth
    MS-Off Ver
    Excel 2010 / Excel 2007
    Posts
    126

    Re: Macro for copying & inserting a variable number of rows into a separate workbook

    Morning!

    Give this a go:

    Please Login or Register  to view this content.
    If my answer helped pls click the star =)

  3. #3
    Forum Contributor
    Join Date
    01-11-2011
    Location
    Frederick, MD, USA
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Macro for copying & inserting a variable number of rows into a separate workbook

    Thanks, cheeze. I'm especially grateful for the commenting - that will help me learn.

    It was throwing an "subscript out of range" error for the line "Workbooks("Origin").Activate"; adding ".xlsm" to it fixed it, though (same thing later, when re-activiating the Archive workbook). So now all is working as expected!

    I did discover that my example worksheets were missing an important feature of the "real" workbooks I'm using, and that is that the "real" workbook uses a series of formula to pull data from other parts of the workbook. (These formula start with a IF statement that evaluates to "" if certain criteria aren't met, so that only the important stuff shows up.) Is there a way around that, so that this macro isn't copying/pasting a bunch of formula or pseudo-empty cells (like ""), but is pasting only "actual" data?

  4. #4
    Forum Contributor
    Join Date
    10-12-2012
    Location
    Bournemouth
    MS-Off Ver
    Excel 2010 / Excel 2007
    Posts
    126

    Re: Macro for copying & inserting a variable number of rows into a separate workbook

    just so I'm clear - all the cells in your Origin sheet have formulas in to ensure it gets the data needed, and the code is copying these as well into your archive with the formulas?

    If that's right, if you change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    although this would select everything in the range, including the formulas it would paste them as values, so the "" cells should be paste as blanks and not recognised as cells with content next time it's paste.....i think

  5. #5
    Forum Contributor
    Join Date
    10-12-2012
    Location
    Bournemouth
    MS-Off Ver
    Excel 2010 / Excel 2007
    Posts
    126

    Re: Macro for copying & inserting a variable number of rows into a separate workbook

    just so I'm clear - all the cells in your Origin sheet have formulas in to ensure it gets the data needed, and the code is copying these as well into your archive with the formulas?

    If that's right, if you change

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    although this would select everything in the range, including the formulas it would paste them as values, so the "" cells should be paste as blanks and not recognised as cells with content next time it's paste.....i think

  6. #6
    Forum Contributor
    Join Date
    01-11-2011
    Location
    Frederick, MD, USA
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Macro for copying & inserting a variable number of rows into a separate workbook

    Hrm...after changing only that one line, I get:

    "Run-time error '1001':
    Application-defined or onject-defined error"


    BUT...changing it to
    Please Login or Register  to view this content.
    now it looks to be working swell!
    Last edited by Gunther Maplethorpe; 11-01-2013 at 05:58 PM.

  7. #7
    Forum Contributor
    Join Date
    01-11-2011
    Location
    Frederick, MD, USA
    MS-Off Ver
    Excel 2007
    Posts
    125

    Re: Macro for copying & inserting a variable number of rows into a separate workbook

    Well, shoot...seems I spoke too soon. Though it's now pasting correctly - that is, it's not pasting formula from cells that return "" - but on successive pastes, it's seeing all those cells that were pasted blank, and skipping them all for the next paste.

    In other words, the first time this macros is used, it pastes ten (say) rows of data, then another 30 (say) lines of what appear to be empty. Then, the next time I use this macro, it skips down to row 40 (after all the 'empty' cells) to paste in the next set.

    How can I make those "empty" cells truly empty, so that they'er no0t 'seen' by the macro the next time?

+ 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. Inserting a variable number of rows throughout a catalog.
    By Common Wealth in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-29-2012, 04:40 PM
  2. Copying a variable number of rows in a macro
    By MichaelMcF in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-17-2012, 11:29 PM
  3. macro inserting and copying rows
    By Guy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2008, 01:52 PM
  4. [SOLVED] Copying range to variable number of rows
    By nospaminlich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-01-2005, 07:40 PM
  5. Inserting Variable Number of Rows
    By bundyloco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2005, 07:05 AM

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