リストボックスの順番を変える、よく使うサブルーチンライブラリ
ListBoxSub.bas
リストボックスの順番の入れ替えは、地味にオペレーションを楽にします。でも移動させるのって案外面倒。
なので、リストボックス内で移動させるブルーチンを作りました。
リストボックス内のソートにも利用できます。
※単純なソート(並び替えアルゴリズム
Dim Data(10)
Dim max
 :
max = 10
For i=0 to max – 1
 For j=i + 1 to max
  If Data(i)>Data(j) then
   w = Data(i)
   Data(i) = Data(j)
   Data(j) = w
  End If
 Next j
Next i
リストの入れ替え
アクティブなリストと指定されたリストを交換します。Sub ListMove(iObj As Object, v)
    ‘iObj:リストボックスオブジェクト
‘v: 交換先リスト、アクティブリストからの相対位置
‘v: 交換先リスト、アクティブリストからの相対位置
使用例、
 
Sub Init()
Dim ssht, sr, sc
Set ssht = ActiveSheet
ListBox1.Clear
sr = 3
sc = 3
Do While ssht.Cells(sr, sc) <> ""
DoEvents
ListBox1.AddItem ssht.Cells(sr, sc)
sr = sr + 1
Loop
End Sub
Private Sub CommandButton1_Click()
'▲
Call ListBoxSub.ListMove(ListBox1, -1)
End Sub
Private Sub CommandButton2_Click()
'▼
Call ListBoxSub.ListMove(ListBox1, 1)
End Sub
結果、
下方向ボタンクリック
 
リストボックスのリストの入れ替え
指定された2つのリストを交換します。書式 Sub ListChange(iObj, i, j)
    ‘iObj:リストボックスオブジェクト
‘i: 交換するリストの番号
‘j: 交換するリストの番号
‘i: 交換するリストの番号
‘j: 交換するリストの番号
使用例、
Dim Data(10)
Dim max
max = ListBox1.ListCount
For i=0 to max – 2
 For j=i + 1 to max – 1
  If ListBox1.List(i)>ListBox1.List(j) then
   Call ListBoxSub.ListChange(ListBox1,i,j)
  End If
 Next j
Next i
ListBox 並び替え
書式 Sub ListSort(iObj, v)
    iObj:リストボックスオブジェクト
v:1:昇順 0以下:降順
v:1:昇順 0以下:降順
使用例、
 
Private Sub CommandButton3_Click()
'昇順▲
Call ListBoxSub.ListSort(ListBox1, 1)
End Sub
Private Sub CommandButton4_Click()
'降順▼
Call ListBoxSub.ListSort(ListBox1, 0)
End Sub
結果、
昇順ボタン押下
 
降順ボタン押下
 
 








