在列表框中select多个图纸名称并在msgbox中返回结果

我尝试了各种方法来select列表框中的多个工作表,并将选定的工作表名称返回到msgbox。 任何人都可以帮助做到这一点。

目前我能够填充列表框中的工作表名称。但是我没有得到msgbox中所有选定的工作表名称。

Public listChoice As String Private Sub UserForm_Activate() For n = 1 To ActiveWorkbook.Sheets.Count With ListBox1 .AddItem ActiveWorkbook.Sheets(n).Name End With Next n End Sub Private Sub ListBox1_AfterUpdate() listChoice = ListBox1.Text End Sub Private Sub CommandButton1_Click() MsgBox (listChoice) End Sub 

在列表框中获取选定的项目并不像你希望的那样简单:

  Private Sub CommandButton1_Click() Dim Msg As String Dim i As Integer Msg = "You selected" & vbNewLine For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then Msg = Msg & ListBox1.List(i) & vbNewLine End If Next i MsgBox Msg End Sub 

信用: http : //www.java2s.com/Code/VBA-Excel-Access-Word/Forms/GettheselecteditemsinaListBox.htm

您不需要ListBox1_AfterUpdate() Sub或公共listChoicevariables与此代码

这个msgbox列表框中的选定项目。

 Private Sub CommandButton1_Click() For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) Then Msg = ListBox1.List(i) & vbNewLine End If Next i MsgBox Msg End Sub 

像这样的东西可能会有所帮助,

 Private strOP As String Private dicSelections As Scripting.Dictionary Private Sub ListBox1_Mouseup(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) Dim strSheet As String strSheet = Me.ListBox1.List(Me.ListBox1.ListIndex) If Me.ListBox1.Selected(Me.ListBox1.ListIndex) Then If Not dicSelections.Exists(strSheet) Then dicSelections.Add strSheet, strSheet Else End If Else If dicSelections.Exists(strSheet) Then dicSelections.Remove strSheet End If End If End Sub Private Sub UserForm_Click() Me.ListBox1.AddItem "one" Me.ListBox1.AddItem "two" Me.ListBox1.AddItem "three" End Sub Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean) Debug.Print "Worksheets selected " & Join(dicSelections.Items(), " & ") End Sub Private Sub UserForm_Initialize() Set dicSelections = New Scripting.Dictionary End Sub