Identifying a Control Type

C

Chrissy

How do I identify what type of control I have on a form

Dim c As Control

For Each c In UserForm1.Controls
if c (is a text box) then
some code
Next c

What goes in the brackets?
Chrissy.
 
R

Rob Bovey

You also need to qualify the object type name with the MSForms type
library or your code will be looking for different set of controls with the
same name in the Excel type library, e.g.:

If TypeOf c Is MSForms.ListBox then

If TypeOf c Is MSForms.TextBox then

etc....

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *
 
A

Arne

I was not aware of the operator(?) TypeOf. Even though VBA
does consider it legal, I cannot find any help on it, and,
what's more, it did not work in an example I had a look at
(identifying a check box control on a form). It may depend
on the sofware versions, I don't know. The following did
work:

Dim c As Control

For Each c In UserForm1.Controls
if TypeName(c)="TextBox" then
some code
Next c
 
R

Rob Bovey

Hi Arne,

This section from the help topic on the If...Then...Else statement
describes the TypeOf operator:

An expression of the form TypeOf objectname Is objecttype. The objectname is
any object reference and objecttype is any valid object type. The expression
is True if objectname is of the object type specified by objecttype;
otherwise it is False.

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *
 

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

Top