Solved

make an open workbook an active workbook

Posted on 2014-03-27
5
405 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 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now