Dim cht as Object Constant Declaration

  • Thread starter Thread starter R Tanner
  • Start date Start date
R

R Tanner

I have the simplest question ever. Why can I not declare this
variable? I think if you can give me a good answer you will solve my
confusion on declaring constants..

Dim cht As Object

cht = Sheets("Cheet4").ChartObjects("Chart 5").Chart


Thanks and sorry I post so many questions...
 
If you declare a variable as an object (including range or worksheet or workbook
or...), then you have to use the Set command:

Set cht = ....
 
It ius an object, so needs setting

Set cht = Sheets("Cheet4").ChartObjects("Chart 5").Chart


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Your current code is using cht as a property of the "Chart 5". If you are
just wanting the Chart Property returned use this below.

Dim cht As Chart

cht = Sheets("Cheet4").ChartObjects("Chart 5").Chart

If you want cht as a Chart Object then use this:

Dim cht As ChartObject

Set cht = Sheets("Cheet4").ChartObjects("Chart 5")

Ryan
 
The value of a Constant is defined when you write the code. You can't modify
the value of a constant at run time. Moreover, your code is missing the Set
statement which is required when you assign any sort of object to a
variable.

Dim CHT As Excel.Chart
Set CHT = Worksheets(1).ChartObjects(1).Chart ' Note 'Set' keyword
Debug.Print CHT.Name

Without the 'Set' keyword, VBA will attempt to find the Default property of
the Chart object, and I don't think there is a default property of a Chart
object.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top