我有一个带有列表框的用户表单但是当我显示用户表单时,列表框是空的但是当我不显示我可以看到项目

时间:2017-09-02 09:55:18

标签: vba

我有一个带有列表框的用户表单的代码但是当我显示用户表单时,列表框是空的,但是当我不显示我可以看到这些项目时。

选项明确

Dim Tableau(0) As String

Dim myForm As Object ' or use 'As VBComponent'
Dim Ctrl As Control

Dim NewButton1 As MSForms.CommandButton
Dim NewButton2 As MSForms.CommandButton

Sub create()

Tableau(0) = "1"

Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
With myForm
    .Properties("Caption") = "Choisir"
    .Properties("Width") = 200
    .Properties("Height") = 140

    .CodeModule.InsertLines 2, "Public sub userform_initialize()" '<--| start writing your "UserForm_Initialize" sub code
    With .Designer.Controls.Add("forms.listbox.1", Name:="ListBox1")

        .Width = 110
        .Enabled = True

        .ListIndex = -1
        .AddItem "Test N x M", 0
        .AddItem "Test Wet strength", 1
        .AddItem "Test Pressure Loss with Fine", 2
        .AddItem "Test Pressure Loss with Coarse", 3

    End With
    .CodeModule.InsertLines 4, "End sub" '<--| finish writing your "UserForm_Initialize" sub code
    Set NewButton1 = myForm.Designer.Controls.Add("Forms.commandbutton.1")
    With NewButton1
        .Name = "OK"
        .Caption = "OK"
        .Accelerator = "M"
        .Top = 20
        .Left = 120
        .Width = 40
        .Height = 20
        .Font.Size = 8
        .Font.Name = "Tahoma"
        .BackStyle = fmBackStyleOpaque
    End With

    Set NewButton2 = myForm.Designer.Controls.Add("Forms.commandbutton.1")
    With NewButton2
        .Name = "CANCEL"
        .Caption = "CANCEL"
        .Accelerator = "M"
        .Top = NewButton1.Top + 20
        .Left = 120
        .Width = 40
        .Height = 20
        .Font.Size = 8
        .Font.Name = "Tahoma"
        .BackStyle = fmBackStyleOpaque

    End With


End With




myForm.CodeModule.InsertLines 20, "Private Sub ListBox1_Click()"
myForm.CodeModule.InsertLines 21, "Me.ListBox1.Show"
myForm.CodeModule.InsertLines 22, "If Me.ListBox1.ListIndex = 0 Then"
myForm.CodeModule.InsertLines 23, " Me.ListBox1.Selected(0) = True"
myForm.CodeModule.InsertLines 24, "End If"
myForm.CodeModule.InsertLines 25, "If Me.ListBox1.ListIndex = 1 Then"
myForm.CodeModule.InsertLines 26, " Me.ListBox1.Selected(1) = True"
myForm.CodeModule.InsertLines 27, "End If"

myForm.CodeModule.InsertLines 28, "If Me.ListBox1.ListIndex = 2 Then"
myForm.CodeModule.InsertLines 29, " Me.ListBox1.Selected(2) = True"
myForm.CodeModule.InsertLines 30, "End If"
myForm.CodeModule.InsertLines 31, "If Me.ListBox1.ListIndex = 3 Then"
myForm.CodeModule.InsertLines 32, " Me.ListBox1.Selected(3) = True"
myForm.CodeModule.InsertLines 33, "End If"
myForm.CodeModule.InsertLines 34, ""
myForm.CodeModule.InsertLines 35, "End Sub"

myForm.CodeModule.InsertLines 36, "Private Sub OK_Click()"

myForm.CodeModule.InsertLines 37, "If Me.ListBox1.Selected(0) = True Then"
myForm.CodeModule.InsertLines 38, " Msgbox(""Item 1 selected"" & vbLf)"
myForm.CodeModule.InsertLines 39, " Call testNxM"
myForm.CodeModule.InsertLines 40, "End If"
myForm.CodeModule.InsertLines 41, "If Me.ListBox1.Selected(1) = True Then"
myForm.CodeModule.InsertLines 42, " Msgbox(""Item 2 selected"" & vbLf)"
myForm.CodeModule.InsertLines 43, " Call testWet"
myForm.CodeModule.InsertLines 44, "End If"
myForm.CodeModule.InsertLines 45, "If Me.ListBox1.Selected(2) = True Then"
myForm.CodeModule.InsertLines 46, " Msgbox(""Item 3 selected"" & vbLf)"
myForm.CodeModule.InsertLines 47, " Call testPLFine"
myForm.CodeModule.InsertLines 48, "End If"
myForm.CodeModule.InsertLines 49, "If Me.ListBox1.Selected(3) = True Then"
myForm.CodeModule.InsertLines 50, " Msgbox(""Item 4 selected"" & vbLf)"
myForm.CodeModule.InsertLines 51, " Call testPLCoarse"
myForm.CodeModule.InsertLines 52, "End If"
myForm.CodeModule.InsertLines 53, "Unload me"
myForm.CodeModule.InsertLines 54, "End Sub"
myForm.CodeModule.InsertLines 55, "Private Sub CANCEL_Click()"
myForm.CodeModule.InsertLines 56, "Unload Me"
myForm.CodeModule.InsertLines 57, "End Sub"

VBA.UserForms.Add(myForm.Name).Show


'ThisWorkbook.VBProject.VBComponents.Remove myForm

End Sub

当我想验证控件和所有内容时,我不会显示用户表单,我可以看到所有项目并可以点击它们但是当我显示时,它只是一个带有2个按钮的用户表单和一个空的列表框

1 个答案:

答案 0 :(得分:0)

嗯,我无法准确回答你为什么没有工作,但我想这与过渡到运行时有关。但是,一个简单的解决方案是在运行时添加到此列表IE添加

MyForm.codemodule.insertlines 58, "Private Sub UserForm_Activate()"
MyForm.codemodule.insertlines 59, "with me.listbox1"
MyForm.codemodule.insertlines 60, " .additem ""blah blah"""
MyForm.codemodule.insertlines 61, "end with"
MyForm.codemodule.insertlines 62, "End Sub"

到你的代码的底部,它应该工作......