hakeの日記

Windows環境でプログラミングの勉強をしています。

EXCEL VBAメモ - 変数

変数宣言の強制

変数名のタイプミスが見つかり易くなる。

Option Explicit

または、「ツール」→「オプション」→「変数の宣言を強制する」

変数の宣言は1つずつ

以下の変数aは、Integerにならない。

Dim a, b As Integer
MsgBox TypeName(a) '-> Empty
MsgBox TypeName(b) '-> Integer

Dim a As Integer, b As Integer '正しい書き方

変数は宣言時に初期化はできない

以下の様に記述する。

Dim a As Integer: a = 0

整数型はLong型(4byte)

Integer型(2byte)は現在のPCでは却って遅くなる場合も。

リテラルの型文字

Integer   %
Long      &
Single    !
Double    #
Currency  @
String    $

スタティック変数の初期化方法

Static foo As Range
If foo Is Nothing Then
  do_initialize 
End If