Solved

make an open workbook an active workbook

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Finding a closest match in Excel 7 47
.Range Set 25 78
Best Excel  formula for  this scenario 2 38
Excel VBA 4 27
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
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 Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

777 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