0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VBA関数とは

VBA (Visual Basic for Applications) には、さまざまな処理を効率化できる組み込み関数が多数用意されています。
この記事では VBAでよく使う関数 をカテゴリ別に紹介します。

VBA関数の基本構文

変数 = 関数名( 引数1, 引数2, ...)
  • 関数名: 使用する関数の名前
  • 引数: 関数に渡す値(引数なしの関数もあり)
  • 戻り値: 関数の結果(変数に代入・判定などで利用)

条件分岐や入れ子でも使える

If 関数名( 1引数, 2引数, ・・・) = ○○○ Then
変数 = 関数名1( 関数名2 ( 1引数, 2引数, ・・・), 2引数, ・・・)

戻り値を使用しない場合

その場合は、引数を()で囲わずに記述します。

関数名 引数1, 引数2, ...

文字列操作

関数名 機能 使用例
Len 文字数を取得 Len("Excel") → 5
Left 左からn文字を取得 Left("Excel", 2) → "Ex"
Right 右からn文字を取得 Right("Excel", 2) → "el"
Mid 中間の文字列を取得 Mid("Excel", 2, 3) → "xce"
InStr 部分文字列の位置を取得 InStr("Excel", "c") → 3
Trim 前後の空白を削除 Trim(" Excel ") → "Excel"
LTrim 左側の空白を削除 LTrim(" Excel ") → "Excel "
RTrim 右側の空白を削除 RTrim(" Excel ") → " Excel"
LCase 小文字に変換 LCase("Excel") → "excel"
UCase 大文字に変換 UCase("Excel") → "EXCEL"
Replace 文字列の置換 Replace("Excel", "c", "012") → "Ex012el"
Format 書式設定 Format(1234567, "#,##0") → "1,234,567"

数値演算

関数名 機能 使用例
Abs 絶対値 Abs(-123) → 123
Int 切り捨て(小さい方) Int(-123.456) → -124
Fix 整数部分 Fix(-123.456) → -123
Round 四捨五入(銀行丸め有り) Round(123.456, 2) → 123.46
Sqr 平方根 Sqr(36) → 6
Rnd 0 以上 1 未満の乱数 Rnd() → 0.7055475

日付/時刻

関数名 機能 使用例 (日時2025/04/01 12:30:10)
Date 現在の日付 Date() → 2025/04/01
Time 現在の時刻 Time() → 12:30:10
Now 現在の日時 Now() → 2025/04/01 12:30:10
Year 日付の年 Year(Date) → 2025
Month 日付の月 Month(Date) → 4
Day 日付の日 Day(Date) → 1
Hour 時刻の時 Hour(Now) → 12
Minute 時刻の分 Minute(Now) → 30
Second 時刻の秒 Second(Now) → 10
DateAdd 日付を加算または減算 DateAdd("m", 1, Date) → 2025/05/01
DateDiff 日付の差 DateDiff("d", "2025/04/01", "2025/04/08") → 7

型変換

関数名 機能 使用例
CStr 文字列に変換 CStr(123) → "123"(String型)
CInt 整数に変換(四捨五入) CInt(123.567) → 124(Integer型)
CDbl 倍精度浮動小数点数に変換 CDbl(1234567890.123456789) → 1234567890.12346(Double型)
CDate 日付型 CDate("2025/04/01") → 2024/09/04(Date型)

その他

関数名 機能 使用例
MsgBox メッセージ表示 MsgBox "完了", vbInformation, "完了"
msgbox.png
InputBox 入力ボックスを表示 InputBox("名前を入力してください", "ユーザー入力")
inputbox.png
IsNumeric 数値判定 IsNumeric(123) → True
IsEmpty 空判定 Dim temp, temp2
temp = "Excel"
IsEmpty(temp) → False
IsEmpty(temp2) → True
IsNull Null判定 Dim temp, temp2
temp = Null
IsNull(temp) → True
IsNull(temp2) → False

参考リンク

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?