Results 1 to 11 of 11

Thread: (Resolved)Loop through labels in VBA Excel2003

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Resolved (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

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Private Sub CommandButton1_Click()
    2.     For Each lbl In Me.Controls
    3.         If TypeName(lbl) = "Label" Then
    4.             lbl.Caption = "Changed"
    5.         End If
    6.     Next
    7. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    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

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    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

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    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

  8. #8
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    Re: Loop through labels in VBA Excel2003

    Thanks for your help

    VB Code:
    1. Dim CWidth(4) As Single
    2. Dim a As Integer
    3. Dim TotalWidth As Single
    4. Dim ColumnHead(4) As String
    5.  
    6. Worksheets(2).Activate
    7. TotalWidth = 0 'rest total width
    8.  
    9. For a = 0 To 4
    10.  
    11.  Columns(a + 1).Select
    12.     ColumnHead(a) = Cells(1, (a + 1)) 'set column header name for labels
    13.     Selection.EntireColumn.AutoFit 'auto fit column to widest value
    14.     CWidth(a) = Selection.ColumnWidth * 5 'set uasable column width for list columns
    15.    
    16.    
    17.                  
    18.      
    19.     With Label1 'NEED TO INDEX THIS LABEL TO SET LABEL 1-5 EXAMPLE...Label(a)
    20.                 .Caption = ColumnHead(a)
    21.                 .Width = CWidth(a)
    22.                 .Height = 20
    23.                 .LefT = LbData.LefT + TotalWidth
    24.                 .Top = 1
    25.     End With
    26.    
    27. TotalWidth = TotalWidth + CWidth(a) 'add to running total for listbox
    28.    
    29.    
    30. Next a

    I do not set option explicit

  10. #10
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    81

    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
  •  



Click Here to Expand Forum to Full Width