Vba UserForm1

Vba UserForm1,vba,combobox,listbox,userform,Vba,Combobox,Listbox,Userform,进一步链接 这篇文章不处理非模态形式;以这种方式调用第二个Userform需要其他逻辑-请参阅 您是否尝试过将MyVal变暗为变量并使用.List?将.List添加到哪个对象?顺便问一下:在将MyVal传递到ComboBox1之前,程序似乎再次运行了“Sub UserForm1\u Initialize()”。最后(我猜当它实际尝试传递值时)它崩溃了。为什么它会再次初始化?我不会卸载/关闭UserForm1。根据@MathieuGuindon的基本文章“UserForm1.Show?”和他关于堆

进一步链接 这篇文章不处理非模态形式;以这种方式调用第二个Userform需要其他逻辑-请参阅

您是否尝试过将
MyVal
变暗为
变量并使用
.List
?将.List添加到哪个对象?顺便问一下:在将MyVal传递到ComboBox1之前,程序似乎再次运行了“Sub UserForm1\u Initialize()”。最后(我猜当它实际尝试传递值时)它崩溃了。为什么它会再次初始化?我不会卸载/关闭UserForm1。根据@MathieuGuindon的基本文章“UserForm1.Show?”和他关于堆栈溢出的大量帖子,我发布了关于用户表单重新初始化问题的答案;希望有助于进一步了解至少这些面临相同问题的读者:-)您是否尝试将
MyVal
变暗为
变量,并使用
.List
?将.List添加到什么对象?顺便问一下:在将MyVal传递到ComboBox1之前,程序运行的是“Sub-UserForm1\u Initialize()”“又来了。最后(我猜当它实际尝试传递值时)它崩溃了。为什么它会再次初始化?我不会卸载/关闭UserForm1。根据@MathieuGuindon的基本文章“UserForm1.Show?”和他关于堆栈溢出的大量帖子,我发布了关于用户表单重新初始化问题的答案;希望能够帮助这些读者进一步了解同样的问题:-)我以前也尝试过,但似乎没有帮助,我担心…你是否在使用
UserForm1.ComboBox1=MyVal时遇到了错误,请参见上面的详细信息。这正是错误行。我以前尝试过,但它似乎没有帮助,我担心…您是否在使用
UserForm1.ComboBox1=MyVal的行中发现了错误,请参见上面的部分详细信息。这正是错误线。
Sub UserForm1_Initialize()
   ComboBox1.AddItem "Item1"
   ComboBox1.AddItem "Item2"
   ComboBox1.AddItem "Item3"
End Sub

Sub ComboBox1_Click()
   If ComboBox1 = "Item3" Then
      Load UserForm2
      UserForm2.show
   End If
End Sub
Sub UserForm2_Initialize()
   ListBox.AddItem "Apple"
   ListBox.AddItem "Pear"
   ListBox.AddItem "Banana"
End Sub

Sub CommandButton1_Click()
   MyVal = ListBox                 'using .Value or .Text in this or the next line doesn't help
   UserForm1.ComboBox1 = MyVal
   Unload Me
End Sub
Public MyVal As String
MyVal = ListBox.Column(0) 'This will store the data in the first column of the selected row
    UserForm1.ComboBox1 = MyVal 
Option Explicit

Sub UserForm_Initialize()             ' << typo: not UserForm1_Initialize !
   ComboBox1.AddItem "Item1"
   ComboBox1.AddItem "Item2"
   ComboBox1.AddItem "Item3"
End Sub

Sub ComboBox1_Click()
   If ComboBox1 = "Item3" Then
      '''      Load UserForm2          ' << don't reference the default instance 
      '''      UserForm2.Show          ' << but
      ComboBox1.List(2) = yourChoice   '    make it a function call returning your choice
   End If
End Sub
Option Explicit

Sub UserForm_Initialize()               ' typo: not UserForm2_Initialize !
   ListBox1.AddItem "Apple"
   ListBox1.AddItem "Pear"
   ListBox1.AddItem "Banana"
End Sub

Sub CommandButton1_Click()
    ' Purpose: assign listbox value to variable (here: global; better: via class property)
    MyVal = ListBox1                    ' equals ListBox1.List(ListBox1.ListIndex)
    '''   UserForm1.ComboBox1 = MyVal   ' << reinitializes the default instance of UF1 !!!
    Unload Me                           ' (better to make the calling function terminate the form instead)
End Sub
Option Explicit

Public MyVal As String                  ' here: global variable

Sub showMain()
With New UserForm1
    .Show vbModeless ' any mode allowed: 0-vbModeless  or 1-vbModal (default)
End With
End Sub

Function yourChoice$()
With New UserForm2
    .Show vbModal   ' or simply: .Show to allow a correct return of the function result
End With
yourChoice = MyVal  ' (a modeless form would execute any code before termination)
End Function