Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Form Objects in an array?

Hello I have 10 labels for example sake let us say they are named

lbl1
lbl2
....
lbl9
lbl10

I need to make certain ones vanish on certain conditions and it would be
easier if I could put them into an array, is there a way to make these
objects but as an array? I think it'd be easier and shorter code to treat
them this way in a loop I'm doing.

Thanks for any help in advance!

Nathan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Form Objects in an array?


Private MyLabels As Collection

Private Sub cmdOK_Click()
MyLabels(1).Visible = False
End Sub

Private Sub UserForm_Activate()
Call Collection_Of_Labels
End Sub

Function Collection_Of_Labels()
Dim Ctrl As MSForms.Control
Dim collLabels As Collection

Set MyLabels = New Collection
For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "Label" Then
MyLabels .Add Ctrl, Ctrl.Name
End If
Next Ctrl

End Function


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"NateBuckley" wrote in message
...
Hello I have 10 labels for example sake let us say they are named

lbl1
lbl2
...
lbl9
lbl10

I need to make certain ones vanish on certain conditions and it would be
easier if I could put them into an array, is there a way to make these
objects but as an array? I think it'd be easier and shorter code to treat
them this way in a loop I'm doing.

Thanks for any help in advance!

Nathan



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default Form Objects in an array?

Thanks, that makes perfect sense, helped me out loads.

Thank you both! :)

"Bob Phillips" wrote:


Private MyLabels As Collection

Private Sub cmdOK_Click()
MyLabels(1).Visible = False
End Sub

Private Sub UserForm_Activate()
Call Collection_Of_Labels
End Sub

Function Collection_Of_Labels()
Dim Ctrl As MSForms.Control
Dim collLabels As Collection

Set MyLabels = New Collection
For Each Ctrl In Me.Controls
If TypeName(Ctrl) = "Label" Then
MyLabels .Add Ctrl, Ctrl.Name
End If
Next Ctrl

End Function


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"NateBuckley" wrote in message
...
Hello I have 10 labels for example sake let us say they are named

lbl1
lbl2
...
lbl9
lbl10

I need to make certain ones vanish on certain conditions and it would be
easier if I could put them into an array, is there a way to make these
objects but as an array? I think it'd be easier and shorter code to treat
them this way in a loop I'm doing.

Thanks for any help in advance!

Nathan




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 421
Default Form Objects in an array?

Hi Nate,

Perhaps, try something like:

'=========
Private Sub UserForm_Initialize()
Dim arr As Variant
Dim i As Long

With Me
arr = VBA.Array(.Label1, _
.Label2, _
.Label3, _
.Label4, _
.Label5, _
.Label6, _
.Label7, _
.Label8, _
.Label9, _
.Label10)
End With

'demo:
For i = LBound(arr) To UBound(arr)
With arr(i)
.Height = 20
.Width = 40
End With
Next i
End Sub
<<=========

However, a better approach for you might
be something like:

'=========
Private Sub UserForm_Initialize()
Dim Ctl As Control

For Each Ctl In Me.Controls
If TypeName(Ctl) = "Label" Then
With Ctl
.Height = 20
.Width = 40
End With
End If
Next Ctl

End Sub
<<=========

Alternatively, if you need an array, you
could load the array like so:

'=========
Private Sub CommandButton1_Click()
Dim Ctl As Control
Dim arr() As MSForms.Label
Dim i As Long

For Each Ctl In Me.Controls
If TypeName(Ctl) = "Label" Then
With Ctl
i = i + 1
ReDim Preserve arr(1 To i)
Set arr(i) = Ctl
End With
End If
Next Ctl

'Test the array
MsgBox arr(2).Caption

End Sub
<<=========


---
Regards.
Norman


"NateBuckley" wrote in message
...
Hello I have 10 labels for example sake let us say they are named

lbl1
lbl2
...
lbl9
lbl10

I need to make certain ones vanish on certain conditions and it would be
easier if I could put them into an array, is there a way to make these
objects but as an array? I think it'd be easier and shorter code to treat
them this way in a loop I'm doing.

Thanks for any help in advance!

Nathan


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
disable objects on form ranswrt Excel Programming 1 April 22nd 08 02:43 PM
Set array of objects to nothing Matthew Pfluger Excel Programming 2 January 21st 08 02:29 PM
How to look at objects in the form mtlpp Excel Programming 2 June 1st 05 04:41 PM
HELP! limit of objects on a form. Liedson31 Excel Programming 7 April 27th 05 02:25 PM
Dynamically Assign Objects to Form Objects. The Vision Thing Excel Programming 2 December 11th 04 04:02 PM


All times are GMT +1. The time now is 09:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"