PDA

View Full Version : find out which control type being used



Thymen
02-16-2006, 12:24 PM
Hi Folks,

been searching for quite some time, but cannot find an answer to my question, so if you know, plse help....

I want to figure out the type of control on a form. I tried the following:

Dim myControl As Control
On Error Resume Next
For Each myControl In UserForm1.Controls
If (myControl.Type = xlButton) Then MsgBox "Button" Else MsgBox "Not a button"
Next myControl

I want to use this kind of code to dump the contents of all controls that are e.g. textboxes (xlTextBox) to cells on a worksheet, while leaving all other control types be. But.... myControl.Type does not work.... Am I overlooking something?

Help greatly appreciated!


Thymen

Jacob Hilderbrand
02-16-2006, 12:40 PM
Try this.


Option Explicit

Private Sub CommandButton1_Click()

Dim Ctrl As Control

For Each Ctrl In UserForm1.Controls
If TypeName(Ctrl) = "TextBox" Then
MsgBox Ctrl.Name
End If
Next

End Sub

Bob Phillips
02-16-2006, 12:51 PM
Dim Ctrl As Control

For Each Ctrl In UserForm1.Controls
If TypeOf Ctrl Is MSForms.CommandButton Then
MsgBox "Button"
Else
MsgBox "Not a button"
End If
Next

Thymen
02-16-2006, 12:53 PM
Works fine! Both samples! Thanks a lot!

Thymen

Jacob Hilderbrand
02-16-2006, 01:22 PM
Glad to help :beerchug:

Take Care

geekgirlau
02-16-2006, 04:40 PM
Thymen, don't forget to mark the thread as Solved - click on "Thread Tools" at the top of the page.

Thymen
02-17-2006, 12:12 AM
Sorry folks, trying to mark this thread as solved.... but the Solved option in the thread tools do not appear to be working. Will try again later from my home system....

Thymen