メインコンテンツへスキップ

Kutools for Office — 1つのスイート、5つのツールで、もっと多くのことを成し遂げましょう。

Excelで2つのリストボックス間でアイテムを迅速に移動するにはどうすればよいですか?

Author Sun Last modified

以下のスクリーンショットに示すように、必要に応じてあるリストボックスから別のリストボックスにアイテムを移動しようとしたことはありますか? ここで、Excelでのこの操作について説明します。

a screenshot showing the listboxes before moving items a screenshot of an arrow a screenshot showing the listboxes after moving items

リストボックス間でアイテムを移動


リストボックス間でアイテムを移動

この作業を完了するのに役立つ組み込み関数はありませんが、VBAコードがあります。

1. まず、 Admin_Listsという名前の新しいシートに、リストボックス内のアイテムとして表示されるデータリストを作成する必要があります。

a screenshot of the source data

2. 次に、これらのデータを選択し、名前ボックスに移動して、それらに ItemListという名前を付けます。 スクリーンショットをご覧ください:

a screenshot of naming the source data in the Name box

3. 次に、2つのリストボックスを含むシートで、開発 > 挿入 > リストボックス(Active X コントロール)をクリックし、2つのリストボックスを描画します。 スクリーンショットをご覧ください:

a screenshot of selecting the List Box control under the Developer tab a screenshot of the right arrow a screenshot showing two created list boxes

開発タブがリボンに非表示になっている場合、Excel 2007/2010/2013リボンで開発タブを表示/表示するにはどうすればよいですか? この記事では、それを表示する方法を説明します。

4. 次に、開発 > 挿入 > コマンドボタン(Active X コントロール)をクリックし、2つのリストボックスの間に4つのボタンを描画します。 スクリーンショットをご覧ください:

a screenshot of selecting command button control a screenshot of a right arrow 1 a screenshot showing the created command buttons

ここで、4つのコマンドボタンを新しい名前で名前変更します。

5. 最初のコマンドボタンを選択し、プロパティをクリックし、プロパティウィンドウで BTN_moveAllRightという名前を付け、キャプションの横にあるテキストボックスに>>と入力します。 スクリーンショットをご覧ください:

a screenshot showing how to changing the properties of the command button

6. ステップ5を繰り返して、最後の3つのコマンドボタンに以下の名前を付け、キャプションに異なる矢印を入力します。 スクリーンショットをご覧ください:

BTN_MoveSelectedRight

BTN_moveAllLeft

BTN_MoveSelectedLeft

a screenshot of the second command button after changing the properties a screenshot of the third command button after changing the properties a screenshot of the fourth command button after changing the properties

7. リストボックスとコマンドボタンを含むシート名を右クリックし、コンテキストメニューからコードの表示を選択します。 スクリーンショットをご覧ください:

a screenshot of opening the VBA code editor

8. 下記のマクロコードをモジュールスクリプトにコピーして貼り付け、コードを保存してMicrosoft Visual Basic for Applicationsウィンドウを閉じます。 スクリーンショットをご覧ください:

VBA: 2つのリストボックス間でアイテムを移動

Private Sub Worksheet_Activate()
'UpdatebyExtendoffice20171117
    Dim xCell As Range
    Dim xRg As Range
    Set xRg = Sheets("Admin_Lists").Range("ItemList")
    Me.ListBox1.Clear
    Me.ListBox2.Clear
    With Me.ListBox1
        .LinkedCell = ""
        .ListFillRange = ""
        For Each xCell In xRg
            If xCell <> "" Then
                .AddItem xCell.Value
            End If
        Next xCell
    End With
    Me.ListBox1.MultiSelect = fmMultiSelectMulti
    Me.ListBox2.MultiSelect = fmMultiSelectMulti
End Sub

Private Sub BTN_MoveSelectedLeft_Click()
    Call moveSigle(Me.ListBox2, Me.ListBox1)
End Sub

Private Sub BTN_MoveSelectedRight_Click()
    Call moveSigle(Me.ListBox1, Me.ListBox2)
End Sub

Private Sub BTN_moveAllLeft_Click()
    Call moveAll(Me.ListBox2, Me.ListBox1)
End Sub

Private Sub BTN_moveAllRight_Click()
    Call moveAll(Me.ListBox1, Me.ListBox2)
End Sub

Sub moveAll(xListBox1 As Object, xListBox2 As Object)
    Dim I As Long
    For I = 0 To xListBox1.ListCount - 1
        xListBox2.AddItem xListBox1.List(I)
    Next I
    xListBox1.Clear
End Sub

Sub moveSigle(xListBox1 As Object, xListBox2 As Object)
    Dim I As Long
    For I = 0 To xListBox1.ListCount - 1
        If I = xListBox1.ListCount Then Exit Sub
        If xListBox1.Selected(I) = True Then
            xListBox2.AddItem xListBox1.List(I)
            xListBox1.RemoveItem I
            I = I - 1
        End If
    Next
End Sub

a screenshot showing how to use the VBA code

9. 別のシートに移動してから、リストボックスを含むシートに戻ります。これで、リストデータが最初のリストボックスに表示されているのがわかります。 コマンドボタンをクリックして、2つのリストボックス間でアイテムを移動します。

a screenshot showing the source data in one list box after running the VBA code

選択範囲を移動

a screenshot of moving items from one list box to another one by one a screenshot of the arrow a screenshot showing two items have been moved to the right list box

すべて移動

a screenshot of marking which command button can be used to move all items from one list to another at the same time a screenshot of arrow 2 a screenshot showing all items are moved from one list box to another at the same time

最高のオフィス業務効率化ツール

🤖 Kutools AI Aide:データ分析を革新します。主な機能:Intelligent Executionコード生成カスタム数式の作成データの分析とグラフの生成Kutools Functionsの呼び出し……
人気の機能重複の検索・ハイライト・重複をマーキング空白行を削除データを失わずに列またはセルを統合丸める……
スーパーLOOKUP複数条件でのVLookup複数値でのVLookup複数シートの検索ファジーマッチ……
高度なドロップダウンリストドロップダウンリストを素早く作成連動ドロップダウンリスト複数選択ドロップダウンリスト……
列マネージャー指定した数の列を追加列の移動非表示列の表示/非表示の切替範囲&列の比較……
注目の機能グリッドフォーカスデザインビュー強化された数式バーワークブック&ワークシートの管理オートテキスト ライブラリ日付ピッカーデータの統合セルの暗号化/復号化リストで電子メールを送信スーパーフィルター特殊フィルタ(太字/斜体/取り消し線などをフィルター)……
トップ15ツールセット12 種類テキストツールテキストの追加特定の文字を削除など)50種類以上のグラフガントチャートなど)40種類以上の便利な数式誕生日に基づいて年齢を計算するなど)19 種類の挿入ツールQRコードの挿入パスから画像の挿入など)12 種類の変換ツール単語に変換する通貨変換など)7種の統合&分割ツール高度な行のマージセルの分割など)… その他多数
Kutoolsはお好みの言語で利用可能 ― 英語、スペイン語、ドイツ語、フランス語、中国語、その他40以上の言語に対応!

Kutools for ExcelでExcelスキルを強化し、これまでにない効率を体感しましょう。 Kutools for Excelは300以上の高度な機能で生産性向上と保存時間を実現します。最も必要な機能はこちらをクリック...


Office TabでOfficeにタブインターフェースを追加し、作業をもっと簡単に

  • Word、Excel、PowerPointでタブによる編集・閲覧を実現
  • 新しいウィンドウを開かず、同じウィンドウの新しいタブで複数のドキュメントを開いたり作成できます。
  • 生産性が50%向上し、毎日のマウスクリック数を何百回も削減!

全てのKutoolsアドインを一つのインストーラーで

Kutools for Officeスイートは、Excel、Word、Outlook、PowerPoint用アドインとOffice Tab Proをまとめて提供。Officeアプリを横断して働くチームに最適です。

Excel Word Outlook Tabs PowerPoint
  • オールインワンスイート — Excel、Word、Outlook、PowerPoint用アドインとOffice Tab Proが含まれます
  • 1つのインストーラー・1つのライセンス —— 数分でセットアップ完了(MSI対応)
  • 一括管理でより効率的 —— Officeアプリ間で快適な生産性を発揮
  • 30日間フル機能お試し —— 登録やクレジットカード不要
  • コストパフォーマンス最適 —— 個別購入よりお得