+ Reply to Thread
Results 1 to 6 of 6

switch between open workbooks within macro

  1. #1
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    switch between open workbooks within macro

    in this code a target file is is opened and saved 3 times which is unnnecessary but I dont know how to change it. After the first instance of "paste" in workbook opened by the macro, rather than close it I would just like to instruct the macro to activate the previous worksheet before continuing.

    This system was OK with a small test file but unusable where it is needed

    Any help appreciated

    I tried with this method at line 21 Windows("c:\users\nigel.MACROOMHAULAGE\My Documents\vehicle costing.xlsm").Activate but no success
    Please Login or Register  to view this content.
    Last edited by nigelog; 05-31-2012 at 04:31 AM.

  2. #2
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: switch between open workbooks within macro

    I managed to switch back to original workbook after 1st paste by using ThisWorkbook.Activate - however when I try to go back to target workbook I get an error "File already open"

    This is complicated slightly because the worksheet in both the source and target worbooks are the same name.

    Can I use ThisWorkbook.Activate / ThatWorkbook.Activate (for want of better descriptions) and if so are these added as Dim instructions.
    Please Login or Register  to view this content.

  3. #3
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: switch between open workbooks within macro

    I have tried naming the two workbooks by various methods but no success, anyone have an idea how to resolve this? Would be much appreciated

    Please Login or Register  to view this content.

  4. #4
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: switch between open workbooks within macro

    I have tried to use the ThisWorkbook.Activate command to refer to the source file (this is where the code is)

    and I tried to set the target file by using
    Please Login or Register  to view this content.
    but i get an error at this line

    I thought that once the code activated the worksheet in the "target file" it would stay there and I could call back to the source file using the Thisworkbook.activate command

    In reality the code completes but after the first paste and return to source file no other data is transferred and the wrong file (the source file) closes. So the first instance of the target file is not remaing active.

    Any ideas because code would be ideal to use in this format
    Last edited by nigelog; 05-30-2012 at 05:45 AM.

  5. #5
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: switch between open workbooks within macro

    Sometimes you have to go with the tools you have!!

    Couldn't work out how to switch back and forth between the open workbooks, and as code was written it required the target file to opened 3 times due to different size source and target ranges.

    So I did the obvious - make the source and target ranges the same size - then only 1 file open, paste is required

    tidied code is as below

    Please Login or Register  to view this content.
    will mark as solved (self solved LOL). Good sounding board to make you think of other options

  6. #6
    Registered User
    Join Date
    05-31-2012
    Location
    East Maitland, NSW, Australia
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: switch between open workbooks within macro

    Try this, it should provide some ideas to help you.

    I've taken your code and modified it.
    I'm still learning myself so i'm sure someone else could refine it much more than me.

    I haven't tested it but this method has worked for me on other projects.


    Please Login or Register  to view this content.

    Although I just tested the code below on two seperate sheets and was able to set values of cells in the active sheet from cells on a sheet in another workbook.

    Please Login or Register  to view this content.
    You could substitute in
    Please Login or Register  to view this content.
    and use a For Next loop to interate through the range and copy the values from the selected range to the destination.

    Forgot to add that the "With" part of the code in your first post is not necessary. The "With" statement allows you to access the methods and properties of the object included in the "With" statement by starting the code line with just a period, saving having to tye the objects name over and over again.

    The "With" statement allows you to put this sort of code together.

    Please Login or Register  to view this content.
    Which is the same as

    Please Login or Register  to view this content.
    Last edited by andrewp; 05-31-2012 at 08:56 AM.

+ 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