2017-09-02 3 views
0

私はリストフォームを持つユーザーフォームのコードを持っていますが、私は項目を見ることができます私は表示されませんが空ですが、私はコントロールし、すべてを検証したい場合は、私はユーザーフォームを表示しませんし、私ができる私はリストボックスを持つユーザーフォームを持っていますが、ユーザーフォームを表示するとリストボックスは空ですが表示されないときは表示されます

ます。Option Explicit

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 

サブ)(作成

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つのボタンと空のリストボックスを持つちょうどuserformです

答えて

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" 

を追加し、それが動作するはずです実行時に、このリストに追加することです

関連する問題