2012-07-17 11 views
7

Tôi muốn tạo biểu mẫu người dùng trong mô-đun bằng cách sử dụng VBA theo lập trình. Tôi là một người mới và thiếu kinh nghiệm nên tôi đã thử vài ví dụ, nhưng họ không hoàn thành các yêu cầu của tôi.Tạo biểu mẫu theo mô đun bằng cách sử dụng vba

Tôi chỉ muốn macro

  • tạo ra một hình thức sử dụng trong vòng một module sử dụng VBA
  • có một ListBox với một số dữ liệu
  • có một CommandButton với một người biết lắng nghe

Đây là mã mà tôi đã sử dụng

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 

Bây giờ tôi không biết cách thêm ListBox và nút vào biểu mẫu với một người nghe.

+0

Đăng nội dung bạn đã thử và vui lòng đọc http://stackoverflow.com/faq FAQ của FAQ. Đây không phải là nơi để có được câu trả lời nhanh, đây là nơi để học hỏi. –

+0

Cập nhật câu hỏi – IConfused

+1

Tạo toàn bộ biểu mẫu động như thế này thường không hoạt động tốt như bạn nghĩ. Trừ khi bạn thực sự không thể thực hiện một phiên bản chung, ít nhất là một số loại xương, điều này sẽ là một heck của rất nhiều công việc không cần thiết. – Brad

Trả lời

21

Sau khi làm việc chăm chỉ, tôi đã tìm thấy câu trả lời rất đơn giản cho câu hỏi của mình. Có thể giúp bạn.

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

Có gì với 'NewFrame'? – Robino

Các vấn đề liên quan