使用 vba 在模块中以编程方式创建表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11519345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating form programmatically in the module using vba
提问by IConfused
I want to create a user form in the module using VBA programmatically. I am a novice and inexperienced so I have tried couple of examples, but they are not fulfilling my requirements.
我想以编程方式使用 VBA 在模块中创建一个用户表单。我是新手,没有经验,所以我尝试了几个例子,但它们不能满足我的要求。
I just want macro that
我只想要宏
- creates a user form within a module using VBA
- has a ListBox with some data
- has a CommandButton with a listener
- 使用 VBA 在模块内创建用户表单
- 有一个包含一些数据的 ListBox
- 有一个带有侦听器的命令按钮
Here is the code which I used
这是我使用的代码
Option Explicit
Sub MakeuserForm()
'Dim CommandButton1 As MsForms.CommandBarButton
'Dim ListBox1 As MsForms.ListBox
Dim UserForm1 As VBComponent
Set UserForm1 = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With UserForm1
.Properties("Height") = 100
.Properties("Width") = 200
On Error Resume Next
.Name = "My Form"
.Properties("Caption") = "This is your user form"
End With
ShowForm
End Sub
Sub ShowForm()
NewForm.Show
End Sub
Now I don't know how to add ListBox and button to the form with a listener.
现在我不知道如何使用侦听器将 ListBox 和按钮添加到表单中。
回答by IConfused
After hardwork I have found a very simple answer to my question. May help you also.
经过努力,我找到了一个非常简单的答案。也可以帮到你。
Sub CreateUserForm()
Dim myForm As Object
Dim NewFrame As MSForms.Frame
Dim NewButton As MSForms.CommandButton
'Dim NewComboBox As MSForms.ComboBox
Dim NewListBox As MSForms.ListBox
'Dim NewTextBox As MSForms.TextBox
'Dim NewLabel As MSForms.Label
'Dim NewOptionButton As MSForms.OptionButton
'Dim NewCheckBox As MSForms.CheckBox
Dim X As Integer
Dim Line As Integer
'This is to stop screen flashing while creating form
Application.VBE.MainWindow.Visible = False
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
'Create the User Form
With myForm
.Properties("Caption") = "New Form"
.Properties("Width") = 300
.Properties("Height") = 270
End With
'Create ListBox
Set NewListBox = myForm.designer.Controls.Add("Forms.listbox.1")
With NewListBox
.Name = "lst_1"
.Top = 10
.Left = 10
.Width = 150
.Height = 230
.Font.Size = 8
.Font.Name = "Tahoma"
.BorderStyle = fmBorderStyleOpaque
.SpecialEffect = fmSpecialEffectSunken
End With
'Create CommandButton Create
Set NewButton = myForm.designer.Controls.Add("Forms.commandbutton.1")
With NewButton
.Name = "cmd_1"
.Caption = "clickMe"
.Accelerator = "M"
.Top = 10
.Left = 200
.Width = 66
.Height = 20
.Font.Size = 8
.Font.Name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
'add code for listBox
lstBoxData = "Data 1,Data 2,Data 3,Data 4"
myForm.codemodule.insertlines 1, "Private Sub UserForm_Initialize()"
myForm.codemodule.insertlines 2, " me.lst_1.addItem ""Data 1"" "
myForm.codemodule.insertlines 3, " me.lst_1.addItem ""Data 2"" "
myForm.codemodule.insertlines 4, " me.lst_1.addItem ""Data 3"" "
myForm.codemodule.insertlines 5, "End Sub"
'add code for Comand Button
myForm.codemodule.insertlines 6, "Private Sub cmd_1_Click()"
myForm.codemodule.insertlines 7, " If me.lst_1.text <>"""" Then"
myForm.codemodule.insertlines 8, " msgbox (""You selected item: "" & me.lst_1.text )"
myForm.codemodule.insertlines 9, " End If"
myForm.codemodule.insertlines 10, "End Sub"
'Show the form
VBA.UserForms.Add(myForm.Name).Show
'Delete the form (Optional)
'ThisWorkbook.VBProject.VBComponents.Remove myForm
End Sub