Dynamically created form
Dim tmpForm As VBComponent Dim tmpComboBox As MSForms.ComboBox, tmpBtn As MSForms.CommandButton Dim selExp As Long Set tmpForm = ThisWorkbook.VBProject.VBComponents.add(vbext_ct_MSForm) With tmpForm .Properties("Caption") = "Выберите пункт" End With Set tmpComboBox = tmpForm.Designer.Controls.add("Forms.ComboBox.1") With tmpComboBox .name = "tmpComboBox" End With Set tmpBtn = tmpForm.Designer.Controls.add("Forms.CommandButton.1") With tmpBtn .Caption = "Выбрать" .name = "BtnChoose" End With With tmpForm.CodeModule .InsertLines 7, "Private Sub UserForm_Activate()" .InsertLines 8, " With me.tmpComboBox" .InsertLines 9, " .AddItem 1" .InsertLines 10, " .AddItem 2" .InsertLines 11, " .ListIndex = 0" .InsertLines 12, " End With" .InsertLines 13, "End Sub" .InsertLines 20, "Private Sub BtnChoose_Click()" .InsertLines 22, "me.hide" .InsertLines 23, "End Sub" End With VBA.UserForms.add(tmpForm.name).Show ' Вот тут нужно как-то получить выбранное значение tmpComboBox ThisWorkbook.VBProject.VBComponents.remove tmpForm Set tmpComboBox = Nothing Set tmpBtn = Nothing Set tmpForm = Nothing How can I get the selected tmpComboBox value? I tried to contact tmpComboBox directly, but after running the temporary form, it doesn’t refer to anything. I tried to create a Public variable in the form - I don’t know how to access it, for any request I get an error that tmpForm does not contain such a property. I wanted to refer to the Controls form family, but it does not have such a family.
tmpComboBoxvalue. But in general - why not create the form right away and open the finished one? - vikttur pmtmpComboBox.ValueortmpComboBox.Text. But not in the form creation procedure. - vikttur pmVBA.UserForms.add(tmpForm.name).Showand takes control. The program gets to the point where I want to get the value only after the form returns control. In my case, this is done by pressing a button. In the form, in principle, you can make it so that the value oftmpComboBoxsaved in some Public variable declared in the module, but I want to do without them. - Edward Izmalkov