Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

make an open workbook an active workbook

Posted on 2014-03-27
5
Medium Priority
?
439 Views
Last Modified: 2014-04-01
can an expert provide me with the vba code to make an open workbook an active workbook.

So, I am in a workbook [lets call it ABC] that has a macro. this workbook needs to refer to another [open] workbook [Lets call that XYZ]

So ABC needs to switch between ABC and XYZ and back from time to time throughout the Macro Process. I thinking that rather than refer to ABC and XYZ I should refer to ThisWorkbook and ????? [XYZ as ??Workbook]

Thanks
0
Comment
Question by:Jagwarman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Ken Butters
ID: 39959256
Question: When you have the two workbooks open... are they running in separate instances of Excel or in the same instance?    

The answer to your question will be quite a bit different if you need to reach across instances of excel.

If the workbooks are in the same instance of excel, then you will only be able to see workbooks one at a time in the same window.

If the workbooks are in different instances of excel, then you can have the both open at the same time in different windows.
0
 

Author Comment

by:Jagwarman
ID: 39959280
Hi ken.

Always in the same instance

Regards
0
 
LVL 39

Accepted Solution

by:
nutsch earned 1000 total points
ID: 39959281
No need to activate or select your workbooks if you work directly on the object with its parent declared, e.g.

sub someSimpleCode
dim wbk1 as workbook, wbk2 as workbook

set wbk1=thisworkbook
set wbk2=workbooks("otherworkbook.xlsm")

wbk1.sheets("Somesheet").range("A1:B12").copy wbk2.sheets("Other sheet").cells(C5)

'will copy a range from one workbook to another
end sub

Open in new window


Makes sense?

Thomas
0
 
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 1000 total points
ID: 39959296
There is a collection called workbooks that belongs to each "Application" or Instance of excel.

You can iterate through workbook in that collection, or you can go to a specific workbook if you know the name of the workbook.

In your case, "ABC.xls" and you want to get a reference to "XYZ.xls", you can do so like this:

Sub test1()
    Dim myWorkbook As Workbook
    
    Set myWorkbook = Workbooks("XYZ.xls")
    
End Sub

Open in new window


If your workbook is in another instance of Excel, then you have to go get that instance of the Excel application first... then you can reference your workbook that way.
Sub test2()
    Dim myWorkbook As Workbook
    
    Dim xlapp As Excel.Application
    
    Set xlapp = GetObject("XYZ.xls").Application
    
    Set myWorkbook = xlapp.Workbooks("XYZ.xls")
    
End Sub

Open in new window


NOTE:

I would avoid if possible using "Activeworkbook"  The reason for this, is because if you are running the code at the same time that you open up another workbook, the new workbook you are opening becomes the new "Activeworkbook", and it will potentially mess up your macro.

It is a lot safer and cleaner to refer to the workbook you want to act upon by name, rather than by "activeworkbook".
0
 

Author Closing Comment

by:Jagwarman
ID: 39968720
both excellent thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question