View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Subject out of range

I think you are making this more difficult for yourself with the creation of
a new instance of Excel.
objExcel is completely independnt of the instance your run the code in, i.e.
the instance running Book1.

As such, this sentence "The opened workbook Book2 runs macro2 which needs
information from the first workbook", does not mean what you think, as Book2
IS the first workbook in that instance (ignoring hidden WBs).

Drop all the objExcel code and open the file in the current instance:
Set objWorkbook = Workbooks.Open("C:\Program\Book2.xls")
etc..

NickHK

"Xentis" wrote in message
ps.com...
In Book1 I run macro1 with the following code:

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False

Set objWorkbook = objExcel.Workbooks.Open("C:\Program\Book2.xls")
Set objWorksheet = objWorkbook.Worksheets(1)

objWorkbook.Application.Run "'Book2.xls'!macro2"

objWorkbook.SaveAs ("Book3.xls")
objExcel.Quit

The opened workbook Book2 runs macro2 which needs information from the
first workbook
It contains the line:

Year = Workbooks("Book1.xls").Worksheets("Sheet1").Range( "A1")

When both files are opened and i run macro2 within Book2 everything
works fine. But when i run macro 1 it gives me a 'Subject out of
range'. Anyone a solution?

Thanks,

Xentis