Reputation: 11
I have tried a lot of solution, without success and now need your help. I have reduces my complete code, for easier understanding.
I have a form with tree fixed optionbuttons, belong to Group1. Named OptionButton2 to Optionbutton4. I want to create one dynamic created optionbutton, named OptionButton1, also in Group1. Then select one of button, values should be written i cells. OptionButton2 to OptionButton4 works fine, but not the code-created button. Someone with superbrain, want to help me? (In final code I will create a loop to create a lot of buttons)
Option Explicit
Dim opSel As String
Private Sub UserForm_initialize()
Dim OptionButton1 As Control
Set OptionButton1 = Controls.Add("Forms.OptionButton.1", "OptionButton1", True)
With OptionButton1
.Name = "OptionButton1"
.Caption = "OptionButton1"
.GroupName = "Group1"
.Value = 0
.Top = 5
.Left = 10
End With
End Sub
Private Sub OptionButton1_Click()
opSel = OptionButton1.GroupName & " " & OptionButton1.Name
Debug.Print "Selected button: " & opSel
ThisWorkbook.Sheets("Blad1").Cells(1, 1).Value = "TEST-Button1"
End Sub
Private Sub OptionButton2_Click()
opSel = OptionButton2.GroupName & " " & OptionButton2.Name
Debug.Print "Selected button: " & opSel
ThisWorkbook.Sheets("Blad1").Cells(2, 1).Value = "TEST-Button2"
End Sub
Private Sub OptionButton3_Click()
opSel = OptionButton3.GroupName & " " & OptionButton3.Name
Debug.Print "Selected button: " & opSel
ThisWorkbook.Sheets("Blad1").Cells(3, 1).Value = "TEST-Button3"
End Sub
Private Sub OptionButton4_Click()
opSel = OptionButton4.GroupName & " " & OptionButton4.Name
Debug.Print "Selected button: " & opSel
ThisWorkbook.Sheets("Blad1").Cells(4, 1).Value = "TEST-Button4"
End Sub
'Private Sub CommandButton1_Click()
'ThisWorkbook.Sheets("Blad1").Columns("A:A").Clear
'End Sub
Upvotes: 0
Views: 45
Reputation: 11
My solution used in a userform 1000 width: I use TAG to catch the value I then want to use later in Class1 module. Edit what to happen in class1 module. In my final solution I will catch and enter values in specific excel cells.
'In class1 module:
Option Explicit
Public WithEvents CmdEvents As MSForms.OptionButton
Private Sub CmdEvents_Click()
Debug.Print Me.CmdEvents.Tag
End Sub
'In form code
Dim cmdArray_1_2() As New Class1
Dim cmdArray_2_1() As New Class1
Dim cmdArray_2_2() As New Class1
Dim cmdArray_3_1() As New Class1
Dim cmdArray_3_2() As New Class1
Dim rng As Range
Dim rcell As Range
Dim Offset
Dim CountRowLoop
Dim topval As String
Dim starttop
Dim offsetcell As String
starttop = 135
topval = 165
offsetcell = 135
CountRowLoop = 1
Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:A" & ThisWorkbook.Sheets("Sheet1").UsedRange.Rows.Count)
For Each rcell In rng.Cells
'--OptionButtonSection1----------------
Dim optArtStat1 As Control, optArtStat2 As Control
Set optArtStat1 = Controls.Add("Forms.OptionButton.1", "optArtStat1_" & CountRowLoop, True)
With optArtStat1
.Name = "optArtStat1_" & CountRowLoop
.GroupName = "optArtStat" & CountRowLoop
.Caption = "Option 1_" & CountRowLoop
.Tag = "TESTING Option:1 Button:1 Row:" & CountRowLoop
.Left = 610
If CountRowLoop = 1 Then
.Top = starttop
End If
If CountRowLoop > 1 Then
.Top = offsetcell
End If
End With
ReDim Preserve cmdArray_1_1(1 To CountRowLoop)
Set cmdArray_1_1(CountRowLoop).CmdEvents = optArtStat1
'--OptionButtonSection2----------------
Set optArtStat2 = Controls.Add("Forms.OptionButton.1", "optArtStat2_" & CountRowLoop, True)
With optArtStat2
.Name = "optArtStat2_" & CountRowLoop
.GroupName = "optArtStat" & CountRowLoop
.Caption = "Option 1_2_" & CountRowLoop
.Tag = "TESTING Option:1 Button:2 Row:" & CountRowLoop
.Left = 610
If CountRowLoop = 1 Then
.Top = starttop + 15
End If
If CountRowLoop > 1 Then
.Top = offsetcell + 15
End If
End With
ReDim Preserve cmdArray_1_2(1 To CountRowLoop)
Set cmdArray_1_2(CountRowLoop).CmdEvents = optArtStat2
Upvotes: 1