Results 1 to 4 of 4

Thread: [RESOLVED] Looping through controls

  1. #1

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Resolved [RESOLVED] Looping through controls

    I'm sure this has been asked many a time, and I'm sure for what is probably a repeat post, but after much attempt I'm still failing at simply looping through all the controls on my form and outputting the name and caption of that object if it is an optionButton, Frame, CheckBox, or Label.

    I currently have the code just for frame looking like this...

    vb Code:
    1. Dim ctl As Control
    2. For Each ctl In Me.Controls
    3.     If TypeOf ctl Is OptionButton Then
    4.         MsgBox (ctl.Caption)
    5.     End If
    6. Next ctl

    This loop appears to iterate but never see a msgbox... The above code works for a frame, but not for option buttons.... Any help would be appreciated! thanks!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Looping through controls

    Try this
    Code:
    Private Sub CommandButton1_Click()
    Dim wks As Worksheet
     Dim OLEObj As OLEObject
     Set wks = Worksheets("Sheet1")
    
     For Each OLEObj In wks.OLEObjects
         If TypeOf OLEObj.Object Is MSForms.OptionButton Then
                MsgBox OLEObj.Name
         End If
     Next
    End Sub

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Looping through controls

    I'm still failing at simply looping through all the controls on my form
    When you are using controls in a Form then you need to try this...

    vb Code:
    1. Private Sub CommandButton1_Click()
    2.     Dim ctl As Control
    3.     For Each ctl In Me.Controls
    4.         If TypeName(ctl) = "OptionButton" Then
    5.             MsgBox ctl.Name
    6.         ElseIf TypeName(ctl) = "CommandButton" Then
    7.             MsgBox ctl.Name
    8.         ElseIf TypeName(ctl) = "Frame" Then
    9.             MsgBox ctl.Name
    10.         End If
    11.     Next
    12. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Hyperactive Member pgag45's Avatar
    Join Date
    Mar 2007
    Location
    Colorado
    Posts
    262

    Re: Looping through controls

    Great Thanks! Used the latter, works perfectly... Thanks to both of you.

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