+ Reply to Thread
Results 1 to 15 of 15

Switching Between two open Workbooks

  1. #1
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Switching Between two open Workbooks

    I have a very easy problem that has me scratching my head. In my Workbook VBA code, I browse for another Workbook and open it. I need to copy and paste cells between the main Workbook and Workbook I have browsed for and now have open. The browsed workbook is called ManualImport and I am able to open it in the code below. Now that it is open I need to activate it. I am successful in coping and paste just after the Workbook is open but in the last section of the code below I get an error when I try to switch back to the Workbook that was browsed for. that error occurs after the last comment "------------copies and paste data-----------

    I have tried the following:

    Workbooks("ManualImport").Activate
    Windows("ManualImport").Activate
    Windows(ManualImport).Activate

    The first is what I have in the VBA code, but all result in errors. What is correct command?

    Thanks in advance!

    Please Login or Register  to view this content.
    Last edited by Bobbbo; 09-23-2015 at 07:05 PM. Reason: Subscribe

  2. #2
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Re: Switching Between two oepn Workbooks

    you need the file ending (.xlsm, .xlsx) as part of the workbook name

    workbooks("fileName.xlsm").activate

  3. #3
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Switching Between two oepn Workbooks

    The open browsed file already has the .xlsm file ending.

  4. #4
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Re: Switching Between two oepn Workbooks

    your line is

    Workbooks("ManualImport").Activate

    it needs the .xls_ extension if the name of the file in ManualImport.xls_

    or it doesn't need quotes if ManualImport is just a variable containing a string that has the extension

    or it needs both if ManualImport is a variable that contains the file name but not the extension

    Workbooks(ManualImport & ".xlsm").Activate

    when you throw

    msgbox ManualImport

    in there, what does it return?
    Last edited by Ouka; 09-22-2015 at 09:59 PM.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Switching Between two oepn Workbooks

    You don't need to activate or select either workbook to copy/paste between them.

    Here's your code without activating/selecting.
    Please Login or Register  to view this content.
    PS I've assumed you are pasting to the workbook the code is in which you set a reference to here.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Switching Between two open Workbooks

    To elaborate on what Norie is saying about activating and selecting.

    If a workbooks is open you don't need to activate it and select a sheet and select a range to copy the cells to another sheet in an open workbook.

    So I have two open workbooks, the one with the code in it can be referred to as ThisWorkbook and will always mean the same workbook no matter which workbook is active.

    The other workbook is named "One.xlsm", you will see by the code that referring to a worksheet then a sheet on the worksheet and then a range on the worksheet is all you need. I can copy data from one to the other, it doesn't matter if I have another open workbook which happens to be the active workbook. You can also assign variables for workbooks and sheets on those workbooks and ranges on those sheets, then by just referring to the range variable I am referring to the workbook, sheet in that workbook and the range all with one variable. At the end you will see that I could have just specified the workbook, sheet and range for each sheet and copied from one to the other as in Norie's example without activating or selecting anything.

    Please Login or Register  to view this content.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  7. #7
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Switching Between two oepn Workbooks

    Thanks Ouka. The file name is a variable as I select the file by browsing for it. Once I find the file I want to open I save that name and path into cell T23. I then assign ManualImport to that file then open it up using this command:

    Please Login or Register  to view this content.
    When I switch to the other workbook called "VBA-WorkBook 205.xlsm" using this command Windows("VBA-WorkBook 205.xlsm").Activate I need a command to get me back to the other already open workbook that has a variable name of ManualImport.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Switching Between two open Workbooks

    You don't need to switch between workbooks to do this sort of thing as long as you reference things properly.

    Did you try my code from post #5?

  9. #9
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Switching Between two open Workbooks

    Thanks Norie and skywriter. Let me play with this and I will tell you the results. The problem lies in the workbook that is temporarily named ManualImport.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Switching Between two open Workbooks

    ManualImport is the path and filename of the workbook the user has selected.

    In this piece of code we set a reference, quickImport, to that workbook and that can be used throughout the rest of the code.
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    04-05-2010
    Location
    Lakewood, IL
    MS-Off Ver
    Excel 2016
    Posts
    384

    Re: Switching Between two open Workbooks

    Well Norie, I tested out your code in #5 and it works perfectly. Even though you gave me more than I needed, what your help resulted in 3 big things
    1. Got the code to do what I wanted
    2. Reduced my total code to accomplish this by 60%
    3. The open workbook where the VBA code resides does not need to be named (hard coded) into the VBA code allowing me to change the file name whenever I like.

    You guys Rock!! When ever I ask a question on this forum I get a great answer!


  12. #12
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Re: Switching Between two oepn Workbooks

    Other posters are right in that you don't need to switch between active workbooks in this case. But sometimes you do. Keep in mind when you do that, if you have programaticly returned a file name with ActiveWorkbook.Name, that returns the name without the file extension.

    eg your full file name is MyExcelWorkbook.xlsx

    Please Login or Register  to view this content.
    if you return theFileName, it will be "MyExcelWorkbook"

    When you use Workbooks(theFileName) you will return an error since it is missing the extension.

    You can add the extension by adding

    Please Login or Register  to view this content.
    and if you don't know what the extension is necessarily going to be in the future you can grab the file extension when you grab the file extension by doing a InStr parse on the full file & path.

    here is some code that helps break down different parts filepath

    Please Login or Register  to view this content.
    from http://www.thespreadsheetguru.com/th...le-path-string

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Switching Between two open Workbooks

    Ouka

    When do you need to switch between workbooks/worksheets?

  14. #14
    Registered User
    Join Date
    06-06-2019
    Location
    Blaine, Minnesota USA
    MS-Off Ver
    2013, 2016
    Posts
    15

    Re: Switching Between two open Workbooks

    Hi skywriter,

    I have a similar situation but with hundreds of copy/paste lines and I don't necessarily want to go about doing a Dim and Set for each variable.
    How do I transfer data from one workbook to another when one of the workbook names changes every time the two workbooks are opened and code is run?

    Let me know if you want me to attach workbooks as an example.
    Thank you in advance.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Switching Between two open Workbooks

    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread

+ 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. Switching Between Workbooks
    By rbirch in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-19-2015, 10:34 AM
  2. Switching between workbooks open in different instances
    By GerPis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2015, 11:03 AM
  3. [SOLVED] Switching Between Open Workbooks In VBA
    By ajocius in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-16-2012, 05:14 AM
  4. Switching between two workbooks
    By Monkeyboyz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-30-2010, 07:01 AM
  5. Switching between open workbooks
    By Sam_Gregson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-05-2009, 05:58 AM
  6. Switching Between Open Workbooks in Excel 2007
    By ExcelTip in forum Tips and Tutorials
    Replies: 0
    Last Post: 10-31-2007, 04:35 AM
  7. Switching between workbooks
    By Jim in forum Excel General
    Replies: 3
    Last Post: 05-06-2005, 12:06 PM

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