How do I reference another workbook in VB?

C

coolwik

Hello,

Say I have this command in a sheet's VB code:

Sheets("Sheet1").Range("A1").Select

Now instead of referencing Sheet1 in the current workbook, I want to
reference Sheet1 in OtherWorkbook.xls.

How can I make this work? Any help would be greatly appreciated! =)

-Mike
 
N

Nick Hodge

Mike

You could do something like this...

Dim wbOther as workbook
Set wbOther = Workbooks.Open( "C:\YourPath\OtherWorkbook.xls")
wbOther.Sheets("Sheet1").Range("A1").Select

If it's open then just

Workbooks("OtherWorkbook").Sheets(......

Will suffice

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
[email protected]
web: www.nickhodge.co.uk
blog (non-tech): www.nickhodge.co.uk/blog/
 
D

Dave Peterson

Just a note about selecting a range. The sheet has to be active and the
workbook has to be active.

wbOther.Sheets("Sheet1").Range("A1").Select

could be:

wbOther.activate
wbOther.Sheets("Sheet1").select
wbOther.Sheets("Sheet1").Range("A1").Select

or
application.goto wbOther.Sheets("Sheet1").Range("A1"), scroll:=true '??
 
Top