+ Reply to Thread
Results 1 to 9 of 9

Open workbook, insert copied rows VBA code

  1. #1
    Forum Contributor
    Join Date
    07-26-2012
    Location
    USA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    351

    Open workbook, insert copied rows VBA code

    Hey everyone, this is my first post here, probably not my last I'm trying to write a simple macro to copy selected rows from one workbook and insert the copied rows into another workbook. The rows I need to select and copy will vary from day to day. Here's my code so far:

    Please Login or Register  to view this content.
    My problem is the first line. Without it, this macro works fine if I already have the workbook I need to copy to open, but I'm trying to insert a little more code so the macro will automatically open up the workbook I'm inserting the copied rows to, then save and close. Obviously I haven't gotten to the save, close part yet, but I'm wondering if anyone could give me a little guidance on what is causing my error with the code above. The workbook opens fine with the above code, but then I get an error saying the "Selection.Insert Shift:=xlDown" line is not allowed because it is attempting to shift cells in a table on my worksheet. Why am I getting this error? The code worked flawlessly before I added the first line of code telling Excel to open my Sent Invoice Summary - 2012 workbook. Ever since adding in that piece of code, it isn't allowing the insert rows line to function. Anyone know what I'm missing or need to adjust? Thanks!

    -Eric
    Last edited by VBA FTW; 07-26-2012 at 06:30 PM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Open workbook, insert copied rows VBA code

    Hey VBA FTW,

    When a book is just opened, nothing is selected - if you want to copy the sheet that opens, you could write:

    Please Login or Register  to view this content.
    and you could write:

    Please Login or Register  to view this content.
    instead of:

    Please Login or Register  to view this content.
    instead of:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Contributor
    Join Date
    07-26-2012
    Location
    USA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    351

    Re: Open workbook, insert copied rows VBA code

    When I open the book, I am already selecting the "Contractors" sheet with

    Please Login or Register  to view this content.
    and then selecting row 4 of that sheet with
    Please Login or Register  to view this content.
    The
    Please Login or Register  to view this content.
    is copying what I have selected from the original workbook. Since the numbers of rows I need to transfer will differ every day, I need to select them with my mouse before enacting the macro...so I'm telling excel to copy my selection with that

    I'm not entirely convinced this is the issue, I won't be able to try your suggestion until tomorrow though
    Last edited by VBA FTW; 07-26-2012 at 06:59 PM.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Open workbook, insert copied rows VBA code

    When you open the book, what do you expect to already be selected?

  5. #5
    Forum Contributor
    Join Date
    07-26-2012
    Location
    USA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    351

    Re: Open workbook, insert copied rows VBA code

    I see your point, but aren't I already selecting Row 4 of the Contractors sheet when I open the book with the code in my above post? Or are you saying I need to select a cell, then select the sheet I want, then the row I want? I really appreciate your input

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Open workbook, insert copied rows VBA code

    Yeah-but what's on the clipboard in the first place-you're only going to insert at row 4 but what you insert is what's on the clipboard!

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Open workbook, insert copied rows VBA code

    maybe
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  8. #8
    Forum Contributor
    Join Date
    07-26-2012
    Location
    USA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    351

    Re: Open workbook, insert copied rows VBA code

    Quote Originally Posted by xladept View Post
    Yeah-but what's on the clipboard in the first place-you're only going to insert at row 4 but what you insert is what's on the clipboard!
    Thank you so much. I'm pretty certain you are correct, there was nothing on the clipboard to paste to the new workbook once opened. I fixed it with by putting
    Please Login or Register  to view this content.
    before
    Please Login or Register  to view this content.
    Works like a charm now. Thanks again

  9. #9
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Open workbook, insert copied rows VBA code

    @ VBA FTW

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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