ListBoxを表示・選択@Excel マクロ・VBA
ユーザフォームにListBoxを配置します。
配置したListBoxにデータを追加し表示します。
ListBoxを選択し、選択されたデータを表示します。
ユーザフォームを作成します。
作成したユーザフォームに、ListBoxとボタンを配置します。
●ListBoxにデータを配置し、ユーザフォームを表示
'ListBoxにデータを配置
UserForm1.ListBox1.AddItem ("A")
UserForm1.ListBox1.AddItem ("B")
UserForm1.ListBox1.AddItem ("C")
'ListBoxを複数選択可能とする
UserForm1.ListBox1.MultiSelect = fmMultiSelectMulti
'UserFromを表示
UserForm1.Show
|
●ユーザフォームに配置したボタン押下時の処理
'ボタン押下時の処理
Private Sub CommandButton1_Click()
Dim i As Integer
'ListBoxに配置されているデータの数でループ
For i = 0 To UserForm1.ListBox1.ListCount - 1
'選択されているデータを表示
If ListBox1.Selected(i) Then
MsgBox (ListBox1.List(i))
End If
Next
End Sub
|