[英]Reading values from dynamically added userform controls vba excel
我有一個excel vba表格,其中動態添加了不同的控件,但是當我嘗試讀取這些所謂的控件的值時,找不到它們。 我的代碼如下所示:
Private Sub CommandButtonAddIndependentParameters_Click() Dim theComboBoxIndependentParameterNameNo As MSForms.ComboBox Dim theLabelIndependentParameterNo As MSForms.Label iii = iii + 1 If iii <= 2 Then Set theComboBoxIndependentParameterNameNo = FrameMeasurement.Controls.Add("Forms.ComboBox.1", _ "ComboBoxIndependentParameterNameNo", True) Set theLabelIndependentParameterNo = FrameMeasurement.Controls.Add("Forms.Label.1", _ "LabelIndependentParameterNo" & iii + 1, True) With theComboBoxIndependentParameterNameNo .Font.Name = "B Nazanin" .Font.Size = 12 .TextAlign = fmTextAlignRight .Height = 24 .Left = 60 .Top = 66 + 40 * (iii) .Width = 100 .AddItem (Range("AN2").Value) .AddItem (Range("AN3").Value) .AddItem (Range("AN4").Value) End With With theLabelIndependentParameterNo .Font.Name = "B Nazanin" .Font.Size = 12 .TextAlign = fmTextAlignRight .Caption = iii + 1 .Width = 14 .Height = 18 .Left = 161 .Top = 66 + 40 * (iii) .AutoSize = False End With End If End Sub
然后,我嘗試使用以下代碼讀取插入的值:
Range("A2").Value = ComboBoxIndependentParameterNameNo2.Value
我認為將代碼應用於該單元格並使用該控件本身時,您不需要代碼在控件旁邊。 嘗試這個:
Range("A2").Value = theComboBoxIndependentParameterNameNo
``我還找到了別人做過的另一個答案:
Dim oneControl As Object
For Each oneControl In userform.Controls
If TypeName(oneControl) = "TextBox" Then
With oneControl
Range("M9") = .Value
End With
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.