使用vba在模块中以编程方式创建表单 [英] Creating form programmatically in the module using vba

查看:172
本文介绍了使用vba在模块中以编程方式创建表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用VBA以编程方式在模块中创建一个用户表单。我是一个新手,没有经验,所以我尝试了几个例子,但是他们没有满足我的要求。



我只想要宏




  • 使用VBA在模块中创建用户表单

  • 具有一些列表框与一些数据

  • 有一个具有监听器的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
。属性(宽度)= 200
错误恢复下一步
.Name =我的窗体
.Properties(Caption)=这是您的用户窗体
结束
ShowForm
End Sub

Sub ShowForm()
NewForm.Show
End Sub
/ pre>

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

解决方案

经过努力,我发现一个非常简单回答我的问题可以帮助你。

  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作为MSForms.Label
'Dim NewOptionButton As MSForms.OptionButton
'Dim NewCheckBox As MSForms.CheckBox
Dim X As Integer
Dim Line As Integer

'这是为了在创建窗体时停止屏幕闪烁
Application.VBE.MainWindow.Visible = False

设置myForm = ThisWorkbook.VBProject.VBComponents.Add(3)

'创建用户表单
使用myForm
.Properties(Caption)=新表单
.Properties(宽)= 300
.Properties(高度)= 270
结束

'创建ListBox
设置NewListBox = myForm.designer.Controls.Add(Forms.listbox.1)
With NewListBox
.Name =lst_1
.To p = 10
.Left = 10
.Width = 150
.Height = 230
.Font.Size = 8
.Font.Name =Tahoma
.BorderStyle = fmBorderStyleOpaque
.SpecialEffect = fmSpecialEffectSunken
结束

'创建CommandButton创建
设置NewButton = myForm.designer.Controls.Add(Forms .commandbutton.1)
NewButton
.Name =cmd_1
.Caption =clickMe
.Accelerator =M
.Top = 10
.Left = 200
.Width = 66
.Height = 20
.Font.Size = 8
.Font.Name =Tahoma
.BackStyle = fmBackStyleOpaque
结束

'为listBox添加代码
lstBoxData =数据1,数据2,数据3,数据4
myForm.codemodule。插入行1,Private Sub UserForm_Initialize()
myForm.codemodule.insertlines 2,me.lst_1.addItemData 1
myForm.codemodule.insertlines 3,me.lst_1。 addItemData 2
myForm.codemodule.insertlines 4,me.lst_1.add项目数据3
myForm.codemodule.insertlines 5,End Sub

'为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(你选定项目:& me.lst_1.text)
myForm.codemodule.insertlines 9,End If
myForm.codemodule.insertlines 10,End Sub
'显示窗体
VBA .UserForms.Add(myForm.Name).Show

'删除表单(可选)
'ThisWorkbook.VBProject.VBComponents.Remove myForm
End Sub


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.

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

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.

解决方案

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

这篇关于使用vba在模块中以编程方式创建表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆