Is a textBox an object?

joeq

Board Regular
Joined
May 31, 2003
Messages
109
Here is what I tried to do. I'm sure this will be a learning experience :)

I have 8 command buttons on a user form that I call "chances" or "strikes".

I wanted to clear out their Captions and wondered if there was a way to deal with them collectively instead of always individually through their userform name.

Creating an array of objects, assigning each button as an object and then working with the objects didn't seem to work.

Run time error 91
Object variable or With block variable not set.

How badly off track am i here? thanks, joe


Public Sub reset_chances()
Dim o As Integer
Dim obj(8) As Object

obj(1) = BoardUserForm1.c_1
obj(2) = BoardUserForm1.c_2
obj(3) = BoardUserForm1.c_3
obj(4) = BoardUserForm1.c_4
obj(5) = BoardUserForm1.c_5
obj(6) = BoardUserForm1.c_6
obj(7) = BoardUserForm1.c_7
obj(8) = BoardUserForm1.c_8

For o = 1 To 8
obj(o).Caption = ""
Next o

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
joe

Are you dealing with textboxes or command buttons?
Code:
Public Sub reset_chances()
Dim o As Integer

For o = 1 To 8
     Me.Controls("c_" & o).Caption = ""
Next o

End Sub
 
Upvote 0
sorry - dealing here with command buttons.

gotcha - so still it seems the best chance of dealing with them collectively is giving them some kind of number suffix and looping like that?

I can't stuff them all into an Object array and deal with the objects?

not complaining mind you - I'll take your example any day.

thanks, joe
 
Upvote 0
joe

You could 'stuff' them into an array of objects.

But why bother when you have access to the Controls collection of the userform, via which you can access all the controls.

If that isn't quite what you are looking for you might want to look into creating a class module.
 
Upvote 0
Norie,

no - the Controls collection seems like just what I was looking for.

I'll look into that more.

thanks - joe
 
Upvote 0
Hi,

talking about collection, this would be what you need
Code:
Dim ctl As MSForms.Control

    For Each ctl In Me.Controls
    If Left(ctl.Name, 2) = "c_" Then ctl.Caption = ""
    Next ctl
another way to go would be
Code:
If TypeName(ctl) = "CommandButton" Then ...
(last code typed off hand, typos possible)
kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,224,878
Messages
6,181,527
Members
453,053
Latest member
DavidKele

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