简体   繁体   English

从动态添加的用户窗体控件中读取值VBA Excel

[英]Reading values from dynamically added userform controls vba excel

I have an excel vba form in which different controls are added dynamically, but when I try to read the values of these so called controls I cannot find them. 我有一个excel vba表格,其中动态添加了不同的控件,但是当我尝试读取这些所谓的控件的值时,找不到它们。 My code looks like this: 我的代码如下所示:

 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 

And then I try to read the inserted value by using the following code: 然后,我尝试使用以下代码读取插入的值:

 Range("A2").Value = ComboBoxIndependentParameterNameNo2.Value 

I do not believe you need the .value in your code next to the control when you apply it to the cell and use the control itself. 我认为将代码应用于该单元格并使用该控件本身时,您不需要代码在控件旁边。 Try this: 尝试这个:

Range("A2").Value = theComboBoxIndependentParameterNameNo


'I also found another answer someone else had done: ``我还找到了别人做过的另一个答案:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM