ldhyob 版主的方法是最實用的
看到這個問題讓我想到以下方法,簡單的修改一下或許也不錯
將程式碼複製到Module中,然後執行 GetOption程序
Sub GetOption()
Dim TempForm 'As VBComponent
Dim NewOptionButton As MSForms.OptionButton
Dim LeftPos As Integer
Dim X As Integer, i As Integer, TopPos As Integer
' 隱藏VBE視窗預防螢幕更新
Application.VBE.MainWindow.Visible = False
' 動態新增UserForm
Set TempForm = ActiveWorkbook.VBProject.VBComponents.Add(3)
' 新增 OptionButtons
LeftPos = 4
k = 1
TopPos = 5
For i = 1 To 3
LeftPos = 4
For j = 1 To 10
'動態新增OptionButton控件
Set NewOptionButton = _
TempForm.Designer.Controls.Add("forms.OptionButton.1")
With NewOptionButton
.Width = 60
.Caption = k & "℃"
.Height = 15
.Left = LeftPos
.Top = TopPos
.Tag = k & "℃"
.AutoSize = True
End With
LeftPos = LeftPos + 30
k = k + 1
Next j
TopPos = i * 20 + 5
Next i
'寫入Click 事件
For i = 1 To 30
With TempForm.CodeModule
X = .CountOfLines
.InsertLines X + 1, "Private Sub OptionButton" & i & "_Click()"
.InsertLines X + 2, " Cells(8, 8) = Me.ActiveControl.Tag"
.InsertLines X + 3, "End Sub"
End With
Next i
With TempForm
.Properties("Caption") = "溫度選項"
.Properties("Width") = LeftPos + 20
.Properties("Height") = TopPos + 20
.Properties("Left") = 160
.Properties("Top") = 150
End With
'顯示窗體
VBA.UserForms.Add(TempForm.Name).Show
'關閉後一櫥窗體
ActiveWorkbook.VBProject.VBComponents.Remove VBComponent:=TempForm
End Sub |