Results 1 to 6 of 6

Thread: [RESOLVED] Help with creating option button controls...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Resolved [RESOLVED] Help with creating option button controls...

    I am trying to create, at runtime on a user form in excel, an optionbutton for each worksheet in a workbook. But for some reason i am getting an error of "Constant expression required" on 'Dim optionbut(p) As OptionButton'

    Does that mean i have to make it a constant? I changed it to "6" to test this and it didnt give an error but then gives me an "Invalid use of the New Keyword" on 'optionbut(i) = New OptionButton'

    What am i doing wrong?



    Code:
    Dim p As Integer
        p = Sheets.Count
        Dim optionbut(p) As OptionButton
        
        For i = 0 To Sheet.Count - 1
            optionbut(i) = New OptionButton
    VB version: Visual Studio 2008-Professional

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

    Re: Help with creating option button controls...

    try this
    vb Code:
    1. Private Sub CommandButton1_Click()
    2.     Dim cControl As Control
    3.     Dim p As Integer, j As Integer
    4.    
    5.     p = Sheets.Count
    6.     j = 20
    7.  
    8.     For i = 1 To p
    9.         Set cControl = Me.Controls.Add("Forms.OptionButton.1", "MyCaption", True)
    10.      
    11.         With cControl
    12.             .Caption = "aaa" 'Change it so that it changes after every loop
    13.             .Width = 200
    14.             .Height = 50
    15.             .Top = 20 + j
    16.             .Left = 20
    17.         End With
    18.        
    19.         j = j + 30
    20.     Next
    21.  
    22. 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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: Help with creating option button controls...

    Koolsid, works perfect...thank you very much!

    But just for my learning experiance, why was my code/way not the best way to do it?
    VB version: Visual Studio 2008-Professional

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

    Re: Help with creating option button controls...

    thats because vba doesn't support control array like

    Option1(0)
    Option1(1)
    Option1(2)

    you could have achieved the same in vb6 with little modifications...
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: [RESOLVED] Help with creating option button controls...

    Koolsid,

    Ok now how do i loop through all the created optionbuttons to see what one is the selected?

    The method im use to using in vb.net isnt working....

    Code:
    For Each Control In Me.Controls
        If (typeof(control) is optionbutton) Then
            If Control.Value = True Then
                sName = Control.Caption
            End If
        End If
    Next
    Next
    VB version: Visual Studio 2008-Professional

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: [RESOLVED] Help with creating option button controls...

    nevermind, figured out i need to use the TypeName method....

    Code:
    For Each Control In Me.Controls
        If TypeName(Control) = "OptionButton" Then
            If Control.Value = True Then
                sName = Control.Caption
            End If
        End If
    Next
    VB version: Visual Studio 2008-Professional

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