+ Reply to Thread
Results 1 to 7 of 7

Copy and Paste Between 2 Workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2003
    Posts
    18

    Copy and Paste Between 2 Workbooks

    Hi all -

    I am trying to open a workbook called 'Dispfri1,"copy some data, then paste into another workbook, called "Test Spreadsheet," using VBA. The problem is, the code I have below is not working. Any advice? Thanks.

    Sub hi()
        Workbooks.Open Filename:="D:\Documents and Settings\03340496\Desktop\SBN\Dispfri1.xls"
        Sheets("SCHED").Select
        
        Range("A51:C59").Select
        Selection.Copy
        
        Windows("Test Spreadsheet.xls").Activate
        Sheets("Sheet1").Select
        Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("F12").Select
        
       
    End Sub
    Last edited by sbnjac80; 07-02-2009 at 09:53 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy and Paste Between 2 Workbooks

    Hi and welcome to the forum. Can you EDIT that first post, then click on GO ADVANCED, highlight all that code, then click the # icon to wrap it in code tags? It's a forum rule and a good one, makes things easier for everyone viewing the threads.

    Here's a little shorter version of your code:
    Sub hi()
    
    Workbooks.Open Filename:="D:\Documents and Settings\03340496\Desktop\SBN\Dispfri1.xls"
    Sheets("SCHED").Select
    Range("A51:C59").Copy
    
    Workbooks("Test Spreadsheet.xls").Sheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
    Range("F12").Select
    
    End Sub
    If you go into the VBEditor and use F8 to step through your original code one line at a time, where does it fail?

    Does my version fail in the same place?

    Does the spot where it fails help you to see what the problem might be?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-19-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Copy and Paste Between 2 Workbooks

    Thanks for your help.

    Your code does not fail until the very last line:

    Range("F12").Select
    But the macro is not performing like it's supposed to. It's almost like it is copying from "Test Spreadsheet", which is blank. When the macro copies, it is supposed to contain the data in Dispfri1, but it's completely blank.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy and Paste Between 2 Workbooks

    Interesting. As you F8 through the formula, can you see:

    1) The Dispfri1 sheet open?
    2) The data get highlighted and copied?

    I can't imagine how a macro would fail on a simple "select" line, unless that cell doesn't exist. Very odd.

  5. #5
    Registered User
    Join Date
    06-19-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Copy and Paste Between 2 Workbooks

    The Dispfri is opening, but it's not copying the range. I think it has something to do with this: when I copy the data I need from Dispfri and paste special into another spreadsheet (without using a macro), I get this dialog box (see attachment), but copying the data into Test Spreadsheet it is not giving me that dialog box.
    Attached Files Attached Files

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy and Paste Between 2 Workbooks

    No way to know from here why you get/need that dialog box at all. I cut/copy/paste sheets from book to book with VBA every day, don't think I've ever seen that come up.

  7. #7
    Registered User
    Join Date
    06-19-2009
    Location
    U.S.
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: Copy and Paste Between 2 Workbooks

    Ok, anyway to know why it's not working?

+ 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