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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.