|
-
Aug 19th, 2008, 10:20 AM
#1
Thread Starter
Addicted Member
[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
-
Aug 19th, 2008, 11:11 AM
#2
Re: Help with creating option button controls...
try this
vb Code:
Private Sub CommandButton1_Click()
Dim cControl As Control
Dim p As Integer, j As Integer
p = Sheets.Count
j = 20
For i = 1 To p
Set cControl = Me.Controls.Add("Forms.OptionButton.1", "MyCaption", True)
With cControl
.Caption = "aaa" 'Change it so that it changes after every loop
.Width = 200
.Height = 50
.Top = 20 + j
.Left = 20
End With
j = j + 30
Next
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
-
Aug 19th, 2008, 11:20 AM
#3
Thread Starter
Addicted Member
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
-
Aug 19th, 2008, 11:25 AM
#4
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
-
Aug 19th, 2008, 03:21 PM
#5
Thread Starter
Addicted Member
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
-
Aug 19th, 2008, 03:24 PM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|