LoginSignup
4
8

More than 5 years have passed since last update.

「&」を使わないDebug.Printの書き方

Posted at

やりたいこと

  • Debug.Print に複数の単語を連結するときに「&」を使わずに連結したい
  • 「&」はタイピングしにくい文字なので回避したい
  • Debug.print でフォーマットを整える面倒を減らしたい

Debug.Printで単語を連結する方法

「&」,「;」, 「,」の三種類の連結子を使って連結できる。
それぞれ、微妙に使い勝手が違う。

使用例

Module1.bas
Public Sub debugPrintTest()

    Const n As Integer = 10
    Const m As Integer = 20
    Const l As Integer = 30
    Const d As Double = 1.234
    Const s As String = "あなたの年齢は?"
    Const s2 As String = "教えません。"

    '文字と文字の連結
    Debug.Print s & s2 '「&」で連結
    Debug.Print s; s2 '「;」で連結
    Debug.Print s, s2 '「,」で連結
    Debug.Print

    '文字と数値の連結
    Debug.Print s & n & "歳です。" '「&」で連結
    Debug.Print s; n; "歳です。" '「;」で連結
    Debug.Print s, n, "歳です。" '「,」で連結
    Debug.Print

    '数値と数値の連結
    Debug.Print n & m & l & d '「&」で連結
    Debug.Print n; m; l; d  '「;」で連結
    Debug.Print n, m, l, d  '「,」で連結
    Debug.Print

    '複数行にわたる連結

'    「&」つなぎではエラー
'    Debug.Print s &
'    (別処理)
'    Debug.Print m

'    「;」つなぎではエラーにならない
    Debug.Print s;
'    (別処理)
    Debug.Print m; "歳です。"

'   「,」つなぎでもエラーにならない
    Debug.Print s,
'    (別処理)
    Debug.Print m, "歳です。"

End Sub

実行結果

Immediate
あなたの年齢は?教えません。
あなたの年齢は?教えません。
あなたの年齢は?             教えません。

あなたの年齢は?10歳です。
あなたの年齢は? 10 歳です。
あなたの年齢は?              10           歳です。

1020301.234
 10  20  30  1.234 
 10            20            30            1.234 

あなたの年齢は? 20 歳です。
あなたの年齢は?              20           歳です。

主な違いまとめ

  • 「&」は文字も数値も隙間なく並べて表示する
  • 「;」は数値のみスペース区切りで表示する
  • 「,」は文字も数値もタブ区切りで表示する
  • 複数行に渡って連結できるのは「;」と「,」
  • 「&」より、「;」や「,」の方がタイピングしやすい

参照

VB6・VBA関数メモ:Debug.Printメソッド

4
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
8