Excelの赤=用語/青=説明から、交互スライドのフラッシュカードPPTを一発生成(Excel VBA)

Excelのデータ(B=FLAG、C=用語、D=説明)を読み取り、FLAG=1の行だけを使って、赤→青→赤→青…と交互に1枚ずつ表示するフラッシュカード形式のPowerPointを自動生成するVBAマクロです。
テキストは左寄せ、表示エリアは広めに配置。生成されたPPTXはExcelと同じフォルダに自動保存されます。

Option Explicit

Sub MakeFlashcards_FromThisExcel_Alternate_SameFolder()
    '―― PowerPoint定数(Late Binding用:数値で定義)
    Const ppLayoutBlank As Long = 12
    Const msoTextOrientationHorizontal As Long = 1
    Const ppAlignLeft As Long = 1
    Const ppSaveAsOpenXMLPresentation As Long = 24

    Dim ws As Worksheet
    Dim last As Long, i As Long
    Dim flagVal As Variant, redText As String, blueText As String
    Dim reds() As String, blues() As String
    Dim redCount As Long, blueCount As Long, r As Long, b As Long

    Dim pptApp As Object, pptPres As Object
    Dim sld As Object, t As Object
    Dim savePath As String, ts As String
    Dim basePath As String

    On Error GoTo EH

    ' 1) Excelの保存場所を取得
    basePath = ThisWorkbook.Path
    If Len(basePath) = 0 Then
        MsgBox "このブックがまだ保存されていません。いったん保存してから実行してください。", vbExclamation
        Exit Sub
    End If

    ' 2) 対象シート(今開いてるシート)
    Set ws = ActiveSheet

    ' 3) データ読み込み(見出し行=1、FLAG=1のみ収集)
    last = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row ' C列の最終行
    redCount = 0: blueCount = 0

    For i = 2 To last
        flagVal = ws.Cells(i, 2).Value                  ' B=FLAG
        redText = Trim$(CStr(ws.Cells(i, 3).Value))     ' C=赤(用語)
        blueText = Trim$(CStr(ws.Cells(i, 4).Value))    ' D=青(説明)
        If flagVal = 1 Then
            If Len(redText) > 0 Then
                redCount = redCount + 1
                ReDim Preserve reds(1 To redCount)
                reds(redCount) = redText
            End If
            If Len(blueText) > 0 Then
                blueCount = blueCount + 1
                ReDim Preserve blues(1 To blueCount)
                blues(blueCount) = blueText
            End If
        End If
    Next i

    If redCount = 0 And blueCount = 0 Then
        MsgBox "FLAG=1 の有効データがありません。", vbExclamation
        Exit Sub
    End If

    ' 4) 新規 PowerPoint を作成
    Set pptApp = CreateObject("PowerPoint.Application")
    pptApp.Visible = True
    Set pptPres = pptApp.Presentations.Add

    ' 5) 赤・青を交互にスライド追加(左寄せ&広いテキストエリア)
    r = 1: b = 1
    Do While r <= redCount Or b <= blueCount
        If r <= redCount Then
            Set sld = pptPres.Slides.Add(pptPres.Slides.Count + 1, ppLayoutBlank)
            Set t = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 150, 820, 300)
            With t.TextFrame.TextRange
                .Text = reds(r)
                .Font.Size = 48
                .Font.Color.RGB = RGB(255, 0, 0)
                .ParagraphFormat.Alignment = ppAlignLeft
            End With
            r = r + 1
        End If

        If b <= blueCount Then
            Set sld = pptPres.Slides.Add(pptPres.Slides.Count + 1, ppLayoutBlank)
            Set t = sld.Shapes.AddTextbox(msoTextOrientationHorizontal, 50, 150, 820, 340)
            With t.TextFrame.TextRange
                .Text = blues(b)
                .Font.Size = 36
                .Font.Color.RGB = RGB(0, 0, 255)
                .ParagraphFormat.Alignment = ppAlignLeft
            End With
            b = b + 1
        End If
    Loop

    ' 6) 同じフォルダに自動保存(PPTX強制)
    ts = Format(Now, "yyyymmdd_HhNnSs")
    savePath = basePath & "\フラッシュカード版_交互_" & ts & ".pptx"
    pptPres.SaveAs savePath, ppSaveAsOpenXMLPresentation

    MsgBox "フラッシュカード作成完了!" & vbCrLf & savePath, vbInformation
    Exit Sub

EH:
    MsgBox "エラー: " & Err.Number & " - " & Err.Description, vbExclamation
End Sub

操作方法(手順)

  1. Excelにデータを用意
    • A: No(任意)
    • B: FLAG(出題する行は1、しない行は空/0)
    • C: 赤(用語)
    • D: 青(説明)
  2. VBAを貼り付け
    • Excelで Alt + F11 → 挿入 → 標準モジュール
    • 下記コードを貼り付けて保存(.xlsm 推奨)
  3. マクロを実行
    • MakeFlashcards_FromThisExcel_Alternate_Desktop を実行
    • PowerPointが起動し、**赤→青→赤→青…**の順でスライドが生成
    • Excelと同じフォルダ上に フラッシュカード版_交互_YYYYMMDD_HHMMSS.pptx が保存されます
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

GoodMorning!

デジタルの海原を冒険しながら、美食の宝を探し求める探検家です。テクノロジーの世界を舞台に、新しい発見を求めて、キーボードの海を横断。そして、実世界では、隅々まで足を運んで、舌鼓を打つ価値のある美味しいお店を見つけ出します。

私の使命は、小さなITの豆知識から始まり、心を満たすグルメスポットの紹介まで、あなたの日常にちょっとしたスパイスを加えること。画面の向こう側から、気軽に楽しめる話題を届けたいのです。ここでは、私が「これは!」と思った技術的な小話や、舌の記憶に残るような食べ物屋さんを紹介していきます。

このWebサイトは、ITとグルメ、二つの世界を融合させた、まさにデジタルと現実の融合点。ふらっと立ち寄って、新たな発見や、ほっこりするような話題で一息ついていただけたら幸いです。知識の海を冒険し、味覚の旅を楽しみましょう。毎日を少しだけ特別なものに変える、そんな情報をお届けします。

GoodMorning!

I am an explorer who ventures across the digital sea in search of gastronomic treasures. In the world of technology, I traverse the sea of keyboards in search of new discoveries. And in the real world, I visit every nook and cranny to find a delicious restaurant worth tantalizing your taste buds.

My mission is to add a little spice to your everyday life, starting with little IT tidbits and ending with foodie spots that fill your heart. I want to bring you topics that you can easily enjoy from the other side of the screen. Here, I'm going to share with you some of the technical tidbits and I will introduce small technical stories and food shops that will leave a lasting impression on your taste buds.

This Web site is truly a fusion point of digital and reality, combining the two worlds of IT and gourmet. I hope you will stop by and take a breather with new discoveries and dusty topics. Come explore the sea of knowledge and enjoy a journey of taste. I will bring you the information that will change your everyday life into something a little more special.

目次