zz log

zaininnari Blog

エクセル(VBA) と イラストレーターで表を作成する

概要

エクセル上で作成した表のテキストデータをイラストレーターのスレッドテキストに流し込むVBAプログラムを作成しました。

環境

  • Microsoft Windows 8 Pro
    • 今回はOS関係なし
  • Adobe Illustrator CS6 64bit
    • スレッドテキストをサポートしていればOK
  • Microsoft Excel 2013 64bit
    • Microsoft Excel 2007, Microsoft Excel 2010 でも動作OK
    • Microsoft Excel 2003 以前は未確認

仕様

  • MIT License
  • 選択したエクセルのセルの書式適応済みテキスト(cell.text)を縦1列にして、クリップボードにコピーを行う

注意点

  • エクセルからイラストレーターへはテキスト情報(書式適応済み)をコピー
    • フォントやフォントサイズはコピーされません
  • スレッドテキストの作成方向によってVBAを使い分ける必要があります
  • テキストの段組みの設定はイラストレーターで行う必要がります
  • 結合セルは考慮されていません

スレッドテキストで表の元を作成

f:id:zainin:20131020173255p:plain

エリアテキストで文字を作成

f:id:zainin:20131020173928p:plain

Ctrl + Shift + M でコピーを行い、Ctrl + D で動作を繰り返すと楽に作成できます。

f:id:zainin:20131020173909p:plain

f:id:zainin:20131020173916p:plain

テキストを選択して、 「書式」→「スレッドテキスト」→「作成」を行いスレッドテキストを作成します。

f:id:zainin:20131020173920p:plain

スレッドテキストが作成されました。

エクセルで表を準備

f:id:zainin:20131020174535p:plain

りんご バナナ
1個 200 ¥100
2個 400 ¥200
3個 600 ¥300
4個 800 ¥400
5個 1,000 ¥500

価格のカンマや円マークは書式でスタイルをつけています。

VBA

エクセルを起動して、Alt + F11 で、VBE を起動します。 メニューより、「挿入」 → 「標準モジュール」を追加します

Option Explicit
' MIT License

Sub illustratorスレッドテキスト用コピー縦方向()

    Dim beginPosRow As Long, beginPosCol As Long
    Dim endPosRow As Long, endPosCol As Long
    Dim index As Long, i As Long, j As Long
    index = 0
    Dim Cb As New DataObject
    Dim currentCell As Range, currentValue As String, buffer As String
    buffer = ""

    Dim Wb As Workbook
    Set Wb = ThisWorkbook
    Dim Ws As Worksheet
    Set Ws = Selection.Parent

    ' --------------------

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    ' --------------------

    beginPosRow = Selection(1).Row
    beginPosCol = Selection(1).Column
    endPosRow = Selection(Selection.Count).Row
    endPosCol = Selection(Selection.Count).Column

    With Ws
        ' 列を縦に並べるため、列からループを開始
        For i = beginPosCol To endPosCol
            For j = beginPosRow To endPosRow
                Set currentCell = .Cells(j, i)
                currentValue = currentCell.Text
                buffer = buffer + currentValue
                If i <> endPosCol Or j <> endPosRow Then
                    buffer = buffer + vbCrLf
                End If
                index = index + 1
            Next j
        Next i
    End With

    With Cb
        ' 変数の値をDataObjectに格納する
        .SetText buffer
        ' DataObjectのデータをクリップボードに格納する
        .PutInClipboard
    End With


    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    MsgBox index & " 件コピーしました"
End Sub

Sub illustratorスレッドテキスト用コピー横方向()

    Dim beginPosRow As Long, beginPosCol As Long
    Dim endPosRow As Long, endPosCol As Long
    Dim index As Long, i As Long, j As Long
    index = 0
    Dim Cb As New DataObject
    Dim currentCell As Range, currentValue As String, buffer As String
    buffer = ""

    Dim Wb As Workbook
    Set Wb = ThisWorkbook
    Dim Ws As Worksheet
    Set Ws = Selection.Parent

    ' --------------------

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    ' --------------------

    beginPosRow = Selection(1).Row
    beginPosCol = Selection(1).Column
    endPosRow = Selection(Selection.Count).Row
    endPosCol = Selection(Selection.Count).Column

    With Ws
        ' 列を横に並べるため、行からループを開始
        For i = beginPosRow To endPosRow
            For j = beginPosCol To endPosCol
                Set currentCell = .Cells(i, j)
                currentValue = currentCell.Text
                buffer = buffer + currentValue
                If j <> endPosCol Or i <> endPosRow Then
                    buffer = buffer + vbCrLf
                End If
                index = index + 1
            Next j
        Next i
    End With

    With Cb
        ' 変数の値をDataObjectに格納する
        .SetText buffer
        ' DataObjectのデータをクリップボードに格納する
        .PutInClipboard
    End With


    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    MsgBox index & " 件コピーしました"
End Sub

実行

f:id:zainin:20131020175436p:plain

表を選択して、マクロの表示から先ほど記述したillustratorスレッドテキスト用コピー縦方向を実行します。 コピーが完了すると下記のようなダイアログが表示されますので、OKを押して消してください。

f:id:zainin:20131020175827p:plain

イラストレーターへ移動して、スレッドテキスト内に既にあるテキストを削除、または、すべて選択します。

f:id:zainin:20131020180217p:plain

Ctrl + V で貼り付けを行います。

f:id:zainin:20131020180221p:plain

エクセルの書式を維持したままイラストレーターにコピーすることができました。