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?

【Excel VBA】CDbl関数|値を倍精度浮動小数点数(Double)型に変換する方法と注意点

0
Last updated at Posted at 2025-07-17

この記事ではCDbl関数の使い方と注意点について解説します。
他のよく使うVBA関数一覧はこちら。

CDbl関数の使い方と注意点

CDbl関数は、指定した値を倍精度浮動小数点数型(Double型)に変換する関数です。
数値や文字列を小数を含む実数として扱いたいときに使用します。

構文

CDbl()
  • : 倍精度浮動小数点数に変換したい任意の値(数値、文字列など)
  • 戻り値 : 指定された式をDouble型の数値に変換した結果

使用例

数値をDouble型に変換する

Sub Sample()
    Dim MyVal As Variant
    MyVal = 123
    
    Debug.Print TypeName(MyVal)
    Debug.Print TypeName(CDbl(MyVal))
End Sub

▶ 出力結果

Integer
Double

小数を含む数値文字列をDouble型に変換する

Sub Sample()
    Dim MyString As String
    MyString = "123.45"
    
    Debug.Print MyString
    Debug.Print TypeName(MyString)
        
    Debug.Print CDbl(MyString)
    Debug.Print TypeName(CDbl(MyString))
End Sub

▶ 出力結果

123.45
String
123.45 
Double

整数をDouble型に変換する

Sub Sample()
    Dim MyInt As Integer
    MyInt = 789
    Debug.Print TypeName(CDbl(MyInt)) ' Double
End Sub

▶ 出力結果

789 
Integer
789 
Double

⚠️注意

文字列は数値として解釈できる形式である必要がある

Sub Sample()
    On Error Resume Next
    Debug.Print CDbl("abc")
    If Err.Number <> 0 Then Debug.Print "変換エラー:" & Err.Description
    On Error GoTo 0
End Sub

▶ 出力結果

変換エラー:型が一致しません。

NullやEmptyを渡すとエラーや0になる

Sub Sample()
    On Error Resume Next
    Debug.Print CDbl(Empty)
    Debug.Print CDbl(Null)
    If Err.Number <> 0 Then Debug.Print "Null変換時のエラー:" & Err.Description
    On Error GoTo 0
End Sub

▶ 出力結果

0 
Null変換時のエラー:Null の使い方が不正です。

オブジェクト型は変換できない(エラー)

Sub Sample()
    Dim Myobj As Object
    Set Myobj = CreateObject("Scripting.Dictionary")

    On Error Resume Next
    Debug.Print CDbl(Myobj)
    If Err.Number <> 0 Then Debug.Print "オブジェクトは変換できません:" & Err.Description
    On Error GoTo 0
End Sub

▶ 出力結果

オブジェクトは変換できません:引数の数が一致していません。または不正なプロパティを指定しています。

関連するVBA関数

  • CInt関数
  • CDate関数
  • CStr関数

その他のVBA関数

【Excel VBA】VBAでよく使う関数一覧&基本の使い方

参考リンク

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?