トップページに戻る
VBA文法
- VBAでセキュリティ上注意する必要がある関数
ネットあるいは他人から受けとったVBAには、以下の関数が使われていないこと、
使われている場合はセキュリティの問題がないか、確認してください
・ shell
本サイトでは、cmd.exe を 実行するために
shellコマンドを使うことがあります。
・ ネットワーク通信
CreateObject("MSXML2.ServerXMLHTTP")
CreateObject("MSXML2.XMLHTTP")
・ ローカルファイルの削除
Kill file_path
あるいは
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile file_path
あるいは
Shell "cmd.exe /c del file_path"
あるいは
Dim wsh As Object
Set wsh = CreateObject("WScript.Shell")
wsh.Run "cmd.exe /c del file_path"
・レジストリの変更
RegWrite
RegDelete
あるいは
Dim wsh As Object
Set wsh = CreateObject("WScript.Shell")
wsh.RegWrite "HKCU\Software\TestKey", "TestValue"
など
- 文の単位
単文:
行末で終了。
_ で継続行にできる
ブロック文: End ~ で終了
コメント文:
' 以降行末までコメント
- サブプロシージャの定義:
Sub func_name()
...
End Sub
- 関数定義:
Function 関数名(引数) As 戻り値の型
' 処理内容
関数名 = 戻り値
End Function
Function AddNumbers(a As Integer, b As Integer) As Integer
AddNumbers = a + b
End Function
- 特殊な変数・値:
・ Nothing:
オブジェクト変数が参照を持たないことを示す
・ Empty: 変数が初期化されていないことを示す
・ Null: 変数が無効なデータを持つことを示す
・ True, False: ブール値
・ vbNullString: 空の文字列。""と同じ動作
・ Missing:
引数が省略されたことを示す。関数呼び出しの引数に使う
ビルトイン定数:
・ vbYes, vbNo, vbOK, vbCancel:
メッセージボックスで押されたボタンの種類
- 変数の未定義・未初期化などの判断:
変数 var が定義されていないか、未初期化の判定: if
VarType(var) = vbEmpty
オブジェクト var
が初期化されていない場合。未定義ではエラーになる: if
var is Nothing
変数 var
が初期化されていない場合。未定義ではエラーになる場合もある:
if var = Empty
if IsNull(var)
- 基本
変数型:
Integer, Long, Single, Double, Currency, Decimal, String,
Boolean, Date, Object, Variant
- 主なオブジェクト型:
・ Application: アプリケーション
・ Workbook: Excelのブック(ファイル)
Application.Workbooks(file_path)
Workbooks(file_path)
・ Worksheet: Excelのワークシート
Worksheets(シート名)
・ Range: シート内のセルまたはセル範囲
Range("A1")
Range("A1:B10")
・ Chart: グラフ
Charts(グラフ名)
・ Documnet: Wordのドキュメント
Documents(file_path)
・ MailItem: Outlookのメールアイテム
MailItem.Subject など
- 変数宣言:
基本変数型の場合には変数宣言は無くてもエラーにはなりませんが、Variant型になります
i = 10
プログラムの保守上、変数宣言をする場合はExplicit宣言をします
Option Explicit
変数宣言:
Dim i As Integer
Dim str As String
Dim vbProj As Object
配列:
Dim arr(5) As Integer ' 要素数6の配列(0~5)
MsgBox arr(0)
Dim arr(2, 3) As Integer ' 2x3の配列
MsgBox arr(0, 0)
Dim arr() As Integer
ReDim arr(5) ' 要素数6の配列を作成
辞書型 (オブジェクト):
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
' オブジェクトの代入では、必ずSetを使う必要があります
dict.Add "key1", "value1"
dict.Add "key2", "value2"
MsgBox dict("key1")
If dict.Exists("key1") Then
dict.Remove "key1"
End If
- 代入:
cmdPath = "cmd.exe"
オブジェクトの代入
Set ch = ActiveChart
- 文字列の連結:
cmd = cmdPath & " /k cd /d """ & dirPath & """"
- 条件分岐:
If 条件 Then
' 条件が真の場合の処理
Else
' 条件が偽の場合の処理
End If
If i > 10 Then
MsgBox "iは10より大きいです"
Else
MsgBox "iは10以下です"
End If
- 繰り返し:
For 変数 = 開始値 To 終了値
' 繰り返し処理
Next 変数
For i = 1 To 10
MsgBox i
Next i
For Each 変数 In コレクションまたは配列
' 繰り返し処理
Next 変数
Dim cell As Range
For Each cell In Range("A1:A10")
cell.Value = "こんにちは"
Next cell
- with文:
With オブジェクト
.プロパティ1 = 値1
.プロパティ2 = 値2
' その他のプロパティやメソッド
End With
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.Zoom = 75
.CenterHorizontally = True
End With
- 比較:
=, <, <=, >, >=, <>:
数値、文字列の比較
StrComp(str1, str2, vbTextCompare):
大文字・小文字を区別しない比較