Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

make an open workbook an active workbook

Posted on 2014-03-27
5
Medium Priority
?
445 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

916 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