Excel VBA では、変数のデータ型を適切に設定することで、処理速度の向上やエラーの防止 につながります。
しかし、異なるデータ型の変数を扱う際に、型が適合しないエラーが発生することもあります。
そのような場合、データ型を適切に変換する(型変換) ことが重要です。
本記事では、Excel VBA におけるデータ型変換の方法と、型変換時の注意点について解説します。
目次
VBA の主なデータ型
VBA で使用される主なデータ型を確認しましょう。
| データ型 | バイト数 | 値の範囲 | 例 |
|---|---|---|---|
| Byte | 1 | 0 ~ 255 | 100 |
| Integer | 2 | -32,768 ~ 32,767 | 12345 |
| Long | 4 | -2,147,483,648 ~ 2,147,483,647 | 100000 |
| Single | 4 | 浮動小数点数(約7桁) | 3.14 |
| Double | 8 | 浮動小数点数(約15桁) | 3.14159265358979 |
| Currency | 8 | 金額計算に適した小数(15桁) | 1000.50 |
| String | 可変 | 文字列 | "Hello" |
| Boolean | 2 | True / False | True |
| Date | 8 | 日付 | #2025/01/01# |
| Variant | 可変 | すべての型を格納可能(非推奨) | 123, "ABC" |
📌 適切なデータ型を使うことで、メモリの無駄を省き、処理を高速化できます。
VBA でのデータ型変換(型変換)
VBA では、異なるデータ型の値を代入すると、自動的に型変換されることがありますが、明示的に型を変換したい場合 は、型変換関数を使用 します。
・数値型に変換する(CInt, CLng, CSng, CDbl, CCur)
【例】数値型への変換
Sub ConvertToNumber()
Dim strValue As String
Dim intValue As Integer
Dim longValue As Long
Dim singleValue As Single
Dim doubleValue As Double
Dim currencyValue As Currency
strValue = "100"
intValue = CInt(strValue) ' Integer 型に変換
longValue = CLng(strValue) ' Long 型に変換
singleValue = CSng(strValue) ' Single 型に変換
doubleValue = CDbl(strValue) ' Double 型に変換
currencyValue = CCur(strValue) ' Currency 型に変換
Debug.Print "Integer: " & intValue
Debug.Print "Long: " & longValue
Debug.Print "Single: " & singleValue
Debug.Print "Double: " & doubleValue
Debug.Print "Currency: " & currencyValue
End Sub
✅ 文字列 "100" を数値型に変換!
✅ 数値の精度に応じて CInt や CDbl を適切に選択!
・ 文字列型(String)に変換する(CStr)
【例】数値や日付を文字列に変換
Sub ConvertToString()
Dim numValue As Double
Dim dateValue As Date
Dim strValue As String
numValue = 123.456
dateValue = #2025/01/01#
strValue = CStr(numValue) ' 数値を文字列に変換
Debug.Print "String: " & strValue
strValue = CStr(dateValue) ' 日付を文字列に変換
Debug.Print "String (Date): " & strValue
End Sub
✅ 数値や日付を CStr で文字列に変換可能!
✅ 文字列にすることで、テキスト処理が容易に!
【VBA】変数を文字列型・整数型・小数点型への型変換関数の使用方法
・ 日付型(Date)に変換する(CDate)
【例】文字列や数値を日付に変換
Sub ConvertToDate()
Dim strDate As String
Dim numDate As Double
Dim dateValue As Date
strDate = "2025/01/01"
numDate = 45250 ' 1900年1月1日からの日数(2025/01/01)
dateValue = CDate(strDate) ' 文字列を日付型に変換
Debug.Print "Date from String: " & dateValue
dateValue = CDate(numDate) ' 数値を日付型に変換
Debug.Print "Date from Number: " & dateValue
End Sub
✅ 日付の文字列 "2025/01/01" を CDate で Date 型に変換!
✅ Excel のシリアル値(45250)も Date に変換可能!
【VBA】日付を判定する方法:IsDate・VarType・DateValue・CDate
・ Boolean 型に変換する(CBool)
【例】数値や文字列を Boolean に変換
Sub ConvertToBoolean()
Dim numValue As Integer
Dim strValue As String
Dim boolValue As Boolean
numValue = 1
strValue = "True"
boolValue = CBool(numValue) ' 数値を Boolean に変換
Debug.Print "Boolean from Number: " & boolValue
boolValue = CBool(strValue) ' 文字列を Boolean に変換
Debug.Print "Boolean from String: " & boolValue
End Sub
✅ 1 や "True" は True に変換!
✅ 0 や "False" は False に変換!
型変換時の注意点
・ 変換できないデータを扱うとエラーになる
Sub TypeMismatchError()
Dim strValue As String
Dim intValue As Integer
strValue = "ABC"
intValue = CInt(strValue) ' エラー発生!
End Sub
✅ 文字列 "ABC" は数値に変換できないため、Type Mismatch エラーが発生!
📌 型変換前に IsNumeric() や IsDate() を使ってチェックするのが安全!
・ Variant 型は非推奨
Variant は どんなデータ型も格納できる便利な型 ですが、処理速度が遅く、メモリを多く消費するため非推奨!
Sub AvoidVariant()
Dim varValue As Variant
varValue = 123
Debug.Print TypeName(varValue) ' "Integer"
End Sub
✅ 型を明示的に指定し、Variant の使用を最小限にする!
まとめ
| 変換関数 | 変換先のデータ型 | 使用例 |
|---|---|---|
CInt() | Integer | CInt("100") → 100 |
CLng() | Long | CLng("100000") → 100000 |
CSng() | Single | CSng("3.14") → 3.14 |
CDbl() | Double | CDbl("3.141592") → 3.141592 |
CCur() | Currency | CCur("1000.50") → 1000.50 |
CStr() | String | CStr(123.45) → "123.45" |
CDate() | Date | CDate("2025/01/01") → #2025/01/01# |
CBool() | Boolean | CBool("True") → True |