Solved

make an open workbook an active workbook

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

760 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

20 Experts available now in Live!

Get 1:1 Help Now