Test if an object property exists without error trapping

Jubinell

Board Regular
Joined
Jan 17, 2008
Messages
166
Hi,

Sometimes in VBA it's necessary to make sure that the property of an object exists before attempting to apply code to it.

For example, I'd like to set the chart title to "Awesome Chart" regardless of whether the chart already has a title or not.

Code:
Sub Add Chart()
Dim C As Chart
 For Each c In ThisWorkbook.Charts
    c.ChartTitle.Characters.Text = "5"
End If
End Sub

However if the chart doesn't have a title the c.charttitle.characters will fail. How do I test to see if .Characters exist before calling its .text property?

I know I can do On Error Goto, but that seems like a back-door approach to a front-door problem.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
On some occasions you will find a complementary property that you can check. For example, in the case of a chart title, use HasTitle.

That said, there's nothing "back door" about using error handling. In fact, as far as I am concerned, it is the most straight forward way of dealing with complex objects in complex environments. Imagine how compext the object model would become if the object designers had to provide a non-error-trap way to check which of the hundreds (or thousands) of properties and methods were valid at any given time.
 
Upvote 0
On some occasions you will find a complementary property that you can check. For example, in the case of a chart title, use HasTitle.

That said, there's nothing "back door" about using error handling. In fact, as far as I am concerned, it is the most straight forward way of dealing with complex objects in complex environments. Imagine how compext the object model would become if the object designers had to provide a non-error-trap way to check which of the hundreds (or thousands) of properties and methods were valid at any given time.

I would respectfully disagree and say that using Error Handling is a hokey way of checking for the existance of a property and reflects a shortcoming in the VBA language. You could try FreeVBCode code snippet: PropertyExists Routine Using TLBINF32.DLL for a way of doing it though.
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,412
Members
452,399
Latest member
oranges

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top