可以单击以打开另一个UserForm的ListBox选择

时间:2014-03-26 02:25:41

标签: excel excel-vba listbox userform vba

1 UserForms(UserForm1)有一个ListBox(ListBox1),在这个ListBox中有3个项目,当我双击第一个项目时,它转到UserForm2,当我双击第二个项目时,它转到UserForm3,当我双击3项时,它转到UserForm4。

2 个答案:

答案 0 :(得分:0)

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

    Dim Obj As Object
    Set Obj = VBA.UserForms.Add("UserForm" & CStr(ListBox1.ListIndex + 2))
    Obj.Show
    Unload Obj

End Sub

请参阅http://www.cpearson.com/Excel/showanyform.htm

答案 1 :(得分:0)

可以使用这些代码(当点击列表框的任何项目时,打开另一个用户窗体。打开的用户窗体的文本框根据列表框点击的项目值填充):

Private Sub ListBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Load UserForm2
UserForm2.TextBox1 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 0)
UserForm2.TextBox2 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 1)
UserForm2.TextBox3 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 2)
UserForm2.TextBox4 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 3)
UserForm2.TextBox5 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 4)
UserForm2.TextBox6 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 5)
UserForm2.TextBox7 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 6)
UserForm2.TextBox8 = VBA.Format(UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 7), "#,##.00")
UserForm2.TextBox9 = VBA.Format(UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 8), "dd.mm.yyyy")
UserForm2.TextBox10 = UserForm1.ListBox2.List(UserForm1.ListBox2.ListIndex, 0)
Unload UserForm1
UserForm2.Show
End Sub

screenshot