2012-07-17 88 views
7

我想以編程方式使用VBA在模塊中創建用戶表單。我是一個新手,沒有經驗,所以我嘗試過幾個例子,但他們沒有滿足我的要求。使用vba以編程方式在模塊中創建表格

我只想宏

  • 使用VBA
  • 一個模塊中創建了一個用戶表單與一些數據
  • 有一個命令按鈕與聽衆

這裏是一個ListBox我用的代碼

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和按鈕添加到具有偵聽器的窗體。

+0

發表您嘗試過的內容,請閱讀常見問題的http://stackoverflow.com/faq。這是一個獲取快速回答的地方,它是一個學習的地方。 – 2012-07-17 09:48:25

+0

更新了問題 – IConfused 2012-07-17 09:58:31

+1

像這樣動態創建整個表單通常效果不如您想象的那樣。除非你真的無法制作一個通用版本,至少是某種形式的骨架,否則這將是一大堆不必要的工作。 – Brad 2012-07-17 13:07:09

回答

21

經過努力,我發現了一個非常簡單的答案,我的問題。也可以幫助你。

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 
+0

「NewFrame」有什麼用? – Robino 2014-10-01 10:24:11