|
-
Dec 19th, 2005, 09:44 PM
#1
Thread Starter
Lively Member
(Resolved)Loop through labels in VBA Excel2003
This should be simple but I am struggling...
On my Form I have 5 labels named Label1, Label2...
I would like use a For...Each loop to set some position and caption properties for each of the labels.
Thanks
Matt
Last edited by M.Nolan; Dec 20th, 2005 at 09:21 PM.
Reason: Resolved
-
Dec 19th, 2005, 09:53 PM
#2
Re: Loop through labels in VBA Excel2003
You can loop through the controls collection and check for the type of control it is.
VB Code:
Private Sub CommandButton1_Click()
For Each lbl In Me.Controls
If TypeName(lbl) = "Label" Then
lbl.Caption = "Changed"
End If
Next
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 19th, 2005, 10:02 PM
#3
Thread Starter
Lively Member
Re: Loop through labels in VBA Excel2003
Please bear with my ingnorance...
Cut/Paste your code to Commandbutton and run...
I get Type mis-match for Lbl ... is this a varfiable I need to declare?
while in debug says Lbl is Nothing
Thanks
Matt
-
Dec 20th, 2005, 06:49 AM
#4
Re: Loop through labels in VBA Excel2003
What other controls are on your userform? I wrote this in Excel 2003 UserForm.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 20th, 2005, 07:43 AM
#5
Thread Starter
Lively Member
Re: Loop through labels in VBA Excel2003
The Form has:
(5) Labels
(2) OptionButtons
(1) Listbox...Multicolumn
(1) Combobox
I want to loop thru using For ... Next not For...Each (my BAD Sorry!)
Thanks for your help
Matt
-
Dec 20th, 2005, 09:01 AM
#6
Frenzied Member
Re: Loop through labels in VBA Excel2003
What is your hangup against the "For Each ... Next" loop? Is this a restriction on a class assignment or something?
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Dec 20th, 2005, 09:44 AM
#7
Thread Starter
Lively Member
Re: Loop through labels in VBA Excel2003
I am using a For ... Next loop to set the column widths in the MultiColumnListBox, I would like to also set the placemenct and caption Properties for the Labels in the same loop ... Label(a).caption = "Hello"
But I'm not sure how to index thru the controls
...purely my own programming (lack of knowledge) limitation, just trying to learn.
Thanks
Matt
-
Dec 20th, 2005, 10:20 AM
#8
Frenzied Member
Re: Loop through labels in VBA Excel2003
Post your relevant code ... especially your loop. Do you have Option Explicit set?
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Dec 20th, 2005, 12:26 PM
#9
Thread Starter
Lively Member
Re: Loop through labels in VBA Excel2003
Thanks for your help
VB Code:
Dim CWidth(4) As Single
Dim a As Integer
Dim TotalWidth As Single
Dim ColumnHead(4) As String
Worksheets(2).Activate
TotalWidth = 0 'rest total width
For a = 0 To 4
Columns(a + 1).Select
ColumnHead(a) = Cells(1, (a + 1)) 'set column header name for labels
Selection.EntireColumn.AutoFit 'auto fit column to widest value
CWidth(a) = Selection.ColumnWidth * 5 'set uasable column width for list columns
With Label1 'NEED TO INDEX THIS LABEL TO SET LABEL 1-5 EXAMPLE...Label(a)
.Caption = ColumnHead(a)
.Width = CWidth(a)
.Height = 20
.LefT = LbData.LefT + TotalWidth
.Top = 1
End With
TotalWidth = TotalWidth + CWidth(a) 'add to running total for listbox
Next a
I do not set option explicit
-
Dec 20th, 2005, 01:51 PM
#10
Frenzied Member
Re: Loop through labels in VBA Excel2003
WARNING: I don't use controls or forms in Excel so I'm on thin ice here, but this should get you closer to a solution.
I think you can scan the controls and parse 'handles' for the controls into an array ... do this early in your program:
Code:
Dim LabelList(4) As Control
Dim lbl As Control
Dim i As Integer
'Scan the controls for Labels
For Each lbl In Me.Controls
'Process only the Label Controls
If TypeName(lbl) = "Label" Then
'This is a Label ... Parse the numeric ID to position it in the List
'Assumes format: Labeln where n is an integer
i = CInt(Mid$(lbl.Caption, 6, 1)) - 1
Set LabelList(i) = lbl 'Thanks to M. Nolan ... (edited)
End If
Next lbl
Now you should have an indexed array of control handles "LabelList(n).Caption" to use in your loop.
Last edited by Webtest; Dec 21st, 2005 at 07:53 AM.
Blessings in abundance,
All the Best,
& ENJOY!
Art . . . . Carlisle, PA . . USA
-
Dec 20th, 2005, 09:14 PM
#11
Thread Starter
Lively Member
Re: Loop through labels in VBA Excel2003
Webtest,
Thanks for your help.
... had to change:
LabelList(i) = lbl to Set LabelList(i) = lbl
Matt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|