Microsoft로 로그인
로그인하거나 계정을 만듭니다.
안녕하세요.
다른 계정을 선택합니다.
계정이 여러 개 있음
로그인할 계정을 선택합니다.
영어
죄송합니다. 이 문서는 귀하의 언어로 사용할 수 없습니다.

For a Microsoft Word 98 Macintosh Edition version of this article, see 201669.

This article describes how to retrieve selected items from a
ListBox control that makes it possible for you to select multiple values.

Summary

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

In a UserForm, when you set the MultiSelect property to 1 - fmMultiSelectMulti for a ListBox control, you can select any number of items from a list. For example, if a list contains the days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday), you can select any, none, or all the items.

To determine the items that are selected, you can use the Selected property of the list box. The Selected property of a list box is an array of values where each value is either True (if the item is selected) or False (if the item is not selected).

For example, if the list contains the seven days of the week and Sunday, Tuesday, and Saturday are selected, the Selected property array would contain the following values:

True, False, True, False, False, False, TrueThis is true because the first item (Sunday) is selected, the second item (Monday) is not selected, the third item (Tuesday) is selected, the fourth through the sixth items (Wednesday, Thursday, and Friday) are not selected, and the seventh item (Saturday) is selected.

Sample Visual Basic procedure

The following macro code provides two methods for using the selected data from the ListBox. The first method uses one selected item at a time, and the second method builds a list of all the selected items.

Note These steps for creating a UserForm in the Visual Basic Editor assume that you have an understanding of Visual Basic for Applications, Microsoft Word, and Microsoft Forms design and tools.

  1. Start the Visual Basic Editor. To do this, press ALT+F11.

  2. If the Properties dialog box is not visible, click Properties on the View menu.

  3. If the Project Explorer window is not visible, click
    Project Explorer on the View menu.

  4. On the Insert menu, click
    UserForm.

  5. Click the ListBox control on the
    Controls Toolbox, and then drag it to the UserForm.

  6. In the Properties-ListBox1 window, change the MultiSelect property to the 1 - fmMultiSelectMulti value.

  7. Click the CommandButton control on the
    Controls Toolbox, and then drag it to the UserForm to put the CommandButton1 control on the UserForm.

  8. Repeat step 7 to put a second
    CommandButton on the UserForm. This puts the
    CommandButton2 control on the UserForm.

  9. Double-click the UserForm to display the Code window for the UserForm.

  10. Press PAGE DOWN, and then type the following macro code for the Userform_Initialize and the CommandButton_Click events:

Private Sub UserForm_Initialize()

'Creates and assigns the Array to the ListBox when the form loads.
Dim mylist As Variant

mylist = Array("Sunday", "Monday", "Tuesday", "Wednesday", _
"Thursday", "Friday", "Saturday")
ListBox1.List = mylist

End Sub

Private Sub CommandButton1_Click()

'First Method: Displays individual selections one at a time.
For x = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(x) = True Then
MsgBox ListBox1.List(x)
End If

Next x
Unload Me

End Sub

Private Sub CommandButton2_Click()

'Second Method: Displays all the items in one message box
Dim msg As String

For x = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(x) = True Then
msg = msg & ListBox1.List(x) & vbCrLf
End If

Next x
MsgBox "You have selected " & vbCrLf & msg
Unload Me

'Uncomment the following two line to insert the variable
'into a document.
'Documents.Add
'Selection.TypeText Text:= msg

End Sub

More Information

도움이 더 필요하세요?

더 많은 옵션을 원하세요?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

이 정보가 유용한가요?

사용 경험에 어떠한 영향을 주었나요?
제출을 누르면 피드백이 Microsoft 제품과 서비스를 개선하는 데 사용됩니다. IT 관리자는 이 데이터를 수집할 수 있습니다. 개인정보처리방침

의견 주셔서 감사합니다!

×