【VBA】Excelのセルを利用したスタック(Pop/Push)の実装サンプルコード

VBA・Officeカテゴリを表すパンダのイラスト VBA・Office

この記事では、Excelのセル(C2:C22)をメモリのスタックと見立てて、VBAでデータの追加(Push)と取り出し(Pop)を行うサンプルコードを紹介します。

スタック(Stack)とは

スタックは、データ構造の一つで後入れ先出し(LIFO: Last In First Out / FILO: First In Last Out)の特性を持ちます。

  • Push: データをスタックの最後尾に追加する操作。
  • Pop: スタックからデータを取り出して削除する操作。

身近な例では、UnixやDOSコマンドの pushd / popd によるディレクトリ履歴の管理などもスタックの仕組みを活用しています。

VBAサンプルコード

以下のコードを標準モジュールに貼り付けて実行します。セル C2:C22 の範囲をスタック領域として利用します。

Option Explicit

Dim DinamicRange As Range
Dim EventNr As Long
Dim ItemsArr(1 To 3)
Dim StoredValue() As Variant

' Push(1回実行すると、C2:C22の範囲にランダムデータが3つ追加されます)
Sub Push()
    Dim J As Integer
    Dim PopulatedCells As Range
    
    Set DinamicRange = Range("C2:C22")
    On Error Resume Next
    With DinamicRange
        If .SpecialCells(xlCellTypeBlanks).Count < 3 Then
            MsgBox "Stack Full", vbExclamation
            Exit Sub
        End If
        
        ' POP用ランダムデータ作成
        Do
            Randomize
            EventNr = Int((100 * Rnd) + 1)
            If Int(EventNr / 2) = (EventNr / 2) Then
                J = J + 1
                ItemsArr(J) = EventNr
            End If
        Loop While J < 3
        
        Set PopulatedCells = .SpecialCells(xlCellTypeConstants)
        If PopulatedCells Is Nothing Then
            .Cells(.Cells.Count) = ItemsArr(1)
            .Cells(.Cells.Count - 1) = ItemsArr(2)
            .Cells(.Cells.Count - 2) = ItemsArr(3)
        Else
            StoredValue = PopulatedCells.Value
            Set PopulatedCells = PopulatedCells.Offset(-3)
            PopulatedCells.Value = StoredValue
            .Cells(.Cells.Count, 1) = ItemsArr(1)
            .Cells(.Cells.Count - 1) = ItemsArr(2)
            .Cells(.Cells.Count - 2) = ItemsArr(3)
        End If
    End With
End Sub

' Pop(1回実行すると、C2:C22の範囲から最も古いデータが取り出されます)
Sub Pop()
    On Error Resume Next
    Dim PopulatedCells As Range
    
    Set DinamicRange = Range("C2:C22")
    With DinamicRange
        Set PopulatedCells = .SpecialCells(xlCellTypeConstants)
        If PopulatedCells Is Nothing Then
            MsgBox "No more Items to pull !", vbCritical
        Else
            StoredValue = PopulatedCells.Value
            Set PopulatedCells = PopulatedCells.Offset(1).Resize(PopulatedCells.Offset(1).Rows.Count)
            PopulatedCells.Value = StoredValue
            PopulatedCells.Cells(1).Offset(-1).ClearContents
            PopulatedCells.Cells(PopulatedCells.Rows.Count).ClearContents
        End If
    End With
End Sub

参考サイト

MrExcel Forum: Excel Stack Queue Visual Basic Applications

この記事の更新履歴

この記事は、生成AIを活用した自動レビュー・更新フローにより内容を見直し、必要な修正を反映しています。

2026年9月12日

  • 変更記事タイトルの重複した「の」の文字を修正しました。
  • 変更ブロックquoteタグで崩れていたVBAコードを、正しいHTMLタグを用いたコードブロック形式に修正しました。
  • 追加スタックの動作環境や使い方、用語の解説などの説明を追加しました。

文書情報

記事タイトル
【VBA】Excelのセルを利用したスタック(Pop/Push)の実装サンプルコード
作成日
更新日
Source URL
https://papanda925.com/?p=545

ライセンス: 本記事のうち、当サイトが権利を有する本文・自作図表は、特記なき限り CC BY 4.0 で利用できます。生成AIを活用して作成・編集した内容を含みます。コードについて、別途ライセンス表示またはリンク先GitHubリポジトリのライセンスがある場合は、その条件を優先します。引用・第三者資料・画像・商標等は本ライセンスの対象外です。 利用ポリシー

タイトルとURLをコピーしました