We help IT Professionals succeed at work.

How to link an object to a specific Excel sheet

altiplano
altiplano asked
on
Hi expersts,

I use the following code from the help file:

    OLE1.Class = "Excel.Sheet"
    OLE1.OLETypeAllowed = acOLELinked
    OLE1.SourceDoc = "C:\Excel\Oletext.xls"
    OLE1.SourceItem = "R1C1:R5C5"
    OLE1.Action = acOLECreateLink

This works just fine except that it I can't figure out how to specify a different sheet as SourceItem
I tried several variations on OLE1.SourceItem = "'Sheet 2'!R1C1:R5C5", but nothing seems to work.

Any suggestions?

thanks,
Michiel
Comment
Watch Question

Why not use a hyperlink?

Commented:
Did you try:  OLE1.SourceItem = "Sheet 2!R1C1:R5C5"

ie.  Remove the single quotes.
Commented:
Sorry and remove the space:

OLE1.SourceItem = "Sheet2!R1C1:R5C5"

Author

Commented:
As so often there was a double problem. The quotes were indeed wrong (but the spaces are OK). The other problem however, was the length of the total string. Apparently there is some maximum length of about 50 (?) characters.

So this did not work:
    l_Sheet = "Some rather long sheet name with spaces"
    OLE1.SourceItem = l_Sheet & "!R1C1:R5C5"

but this did:
    l_Sheet = "Short name"
    OLE1.SourceItem = l_Sheet & "!R1C1:R5C5"