【问题标题】:Creating form programmatically in the module using vba使用 vba 在模块中以编程方式创建表单
【发布时间】:2012-07-17 09:10:08
【问题描述】:

我想以编程方式使用 VBA 在模块中创建用户表单。我是新手且缺乏经验,因此我尝试了几个示例,但它们不符合我的要求。

我只想要那个宏

  • 使用 VBA 在模块内创建用户表单
  • 有一个包含一些数据的 ListBox
  • 有一个带监听器的 CommandButton

这是我使用的代码

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 

现在我不知道如何使用监听器将 ListBox 和按钮添加到表单中。

【问题讨论】:

  • 发布您尝试过的内容,并请阅读常见问题解答stackoverflow.com/faq。这里不是获取快速答案的地方,而是学习的地方。
  • 像这样动态创建整个表单通常不会像您想象的那么好。除非你真的无法制作通用版本,至少是某种骨架,否则这将是一大堆不必要的工作。
  • 请解释为什么需要动态创建表单。除非您有特殊需要,正如 Brad 解释的那样,这是很多工作。
  • 我想动态地制作它只是因为我想创建一个插件,以便其他用户可以轻松地将该插件导入他们的 Excel 工作表中。 (((如果我创建用户表单,他们可能还必须创建用户表单才能使用我的插件。))

标签: vba excel


【解决方案1】:

经过努力,我找到了一个非常简单的问题答案。也可以帮到你。

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

【讨论】:

  • NewFrame 是怎么回事?
  • 注意,命令按钮需要一个事件处理程序,因此必须使用insertlines 进行硬编码。然而,UserForm_Initialize 例程中的任何内容实际上都可以介于VBA.UserForms.Add(myForm.Name)Show 之间。因此,对于填充列表Set newUf = VBA.UserForms.Add(myForm.Name),然后循环执行newUf.lst_1.addItem 的对象列表比将代码硬编码为字符串要容易得多。最后newUf.Show 全部设置完毕
猜你喜欢
  • 2014-10-19
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
相关资源
最近更新 更多