Trouble activating the worksheet

  • Thread starter Thread starter abhiram
  • Start date Start date
A

abhiram

Hi,

Could someone please tell me in Excel VB Programming, why

Workbooks("excel file").Worksheets("matrix").Activate
x = Range("D" & 20).Value

The above command works, and the below command doesn't
work. it gives an "subscript out of range : runtime error
9".

Workbooks("C:\Project Files\excel file.xls").Worksheets
("matrix").Activate
x = Range("D" & 20).Value

I need a solution to this.

I need to know asap. I would appreciate any help.

Thanks
Abhi.
 
this will work if yourworkbook is open

Sub getavalue()
x = Workbooks("yourworkbook.xls").Sheets("yoursheet").Range("d20")
MsgBox x
End Sub
==========
Sub openandgetavalue()
On Error GoTo ErrorHandler
Workbooks("yourworkbook.xls").Activate
'The following line will halt the macro before the error handler
'is reached.
Exit Sub
ErrorHandler:
Workbooks.Open Filename:="yourworkbook.xls"
x = Workbooks("yourworkbook.xls").Sheets("yourworksheet").Range("b5")
MsgBox x
End Sub
 
Back
Top