Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel called from Visual basic

Status
Not open for further replies.

carlasilva

Programmer
Jan 6, 2003
12
0
0
PT
Why when writing the code below, Visual basic indicates an error in the third line? if it was Set xlSheet = xlBook.Worksheets(1) or Set xlSheet =xlBook.Worksheets(2) or Set xlSheet =xlBook.Worksheets(3) he accpet.

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(4)
xlBook.Worksheets(4).Name = "T"
xlSheet.Cells(1, 1).Value = "n"


Thank you very much.
 
I'm guess you have to add a 4th worksheet before you can reference it.
 
Agreeing with ponerse. Three sheets is the default for new workbooks. You can change this though in Tools > OPtions > General Tab.
Just curious as to why you would have
Set xlSheet = xlBook.Worksheets(4)
which gives the object xlSheet the ref to xlBook.Worksheets(4)
and then in the next line you have
xlBook.Worksheets(4).Name = "T"
why not simply
xlSheet.Name = "T" ?

hope this helps,
sdraper
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top