+ Reply to Thread
Results 1 to 2 of 2

VBA can't open external workbook and copy data at the same time

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2022
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    28

    VBA can't open external workbook and copy data at the same time

    Hi all,
    Odd situation here
    I have the followiing VBA code working well when i open the Quote register file.

    line_backup = Workbooks("Quote Register.xlsx").Worksheets("Data").Range("A2").End(xlDown).Row() + 1
    Workbooks("Quote Register.xlsx").Worksheets("Data").Range("A" & line_backup).Value = Range("QuoteNo1") & Range("Rev").Value
    However when i try automate it and have the same VBA code open the file with the code below it wont work. The file opens but that's about it

    Workbooks.Open "C:\Users\me\Documents\Quote Register.xlsx"
    
    line_backup = Workbooks("Quote Register.xlsx").Worksheets("Data").Range("A2").End(xlDown).Row() + 1
    Workbooks("Quote Register.xlsx").Worksheets("Data").Range("A" & line_backup).Value = Range("QuoteNo1") & Range("Rev").Value

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,977

    Re: VBA can't open external workbook and copy data at the same time

    I cannot say for sure without your file, but it looks like you have this code in a standard module (e.g., Module1). This means that all unqualified references default to ActiveWorkbook. When you open a new file, that file becomes ActiveWorkbook. However, it appears that you intend these to reference ThisWorkbook. This is why I discourage unqualified references in a standard module under most circumstances.

    I would think if my diagnosis is correct, you would be getting error messages, but you don't mention that. But if you qualify the references this may start working for you.

    Also, sometimes an error is not where you think it is, and it is always best to provide all of the code, not just the lines where you think there is a problem. And even better, provide the file for testing.

    Change sheet name to match actual sheet name.

    Workbooks.Open "C:\Users\me\Documents\Quote Register.xlsx"
    
    line_backup = Workbooks("Quote Register.xlsx").Worksheets("Data").Range("A2").End(xlDown).Row() + 1
    Workbooks("Quote Register.xlsx").Worksheets("Data").Range("A" & line_backup).Value = ThisWorkbook.Worksheets("Sample Sheet").Range("QuoteNo1") & ThisWorkbook.Worksheets("Sample Sheet").Range("Rev").Value
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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. Copy Range of Data from Closed Workbook into Open Workbook, Pasting in Last Row
    By Theken67 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2021, 07:17 PM
  2. [SOLVED] VBA for Every 15 min workbook will open and save data and close (for external links update
    By rajeshn_in in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2017, 06:52 PM
  3. [SOLVED] Open external workbook and copy paste from it
    By colin.dickson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-09-2015, 10:12 AM
  4. Replies: 0
    Last Post: 05-27-2013, 05:28 AM
  5. Replies: 6
    Last Post: 01-29-2013, 07:01 AM
  6. [SOLVED] Macro to copy data from open workbook to next open line of new workbook
    By nhtodd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2012, 09:42 PM
  7. External data query leaves datasource workbook open
    By WhytheQ in forum Excel General
    Replies: 0
    Last Post: 05-04-2006, 11:00 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