How do I reference another workbook in VB?

  • Thread starter Thread starter cW
  • Start date Start date
C

cW

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
 
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
(e-mail address removed)DTHIS
web: www.nickhodge.co.uk
blog (non-tech): www.nickhodge.co.uk/blog/


wrote in message
news:[email protected]...
 
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 '??



Nick said:
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
(e-mail address removed)DTHIS
web: www.nickhodge.co.uk
blog (non-tech): www.nickhodge.co.uk/blog/

wrote in message
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
 
Back
Top