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】CInt関数|数値を整数(Integer)型に変換する方法と注意点

Last updated at Posted at 2025-07-16

CInt関数の使い方と注意点

CInt関数は、指定した値を整数型(Integer型)に変換する関数です。
主に小数点以下を四捨五入して整数に変換したいときに使います。

構文

CInt()
  • : 整数に変換したい任意の値(数値、文字列など)
  • 戻り値 : 指定された式を整数(Integer型)に変換した結果

使用例

数値の小数点以下を四捨五入して整数に変換する

Sub Sample()
    Dim Myval1 As Double
    Myval1 = 123.4
    Debug.Print CInt(Myval1)

    Dim Myval2 As Double
    Myval2 = 123.5
    Debug.Print CInt(Myval2)
End Sub

▶ 出力結果

123 
124

文字列の数値を整数に変換する

Sub Sample()
    Dim Mystring As String
    Mystring = "456"
    Debug.Print CInt(Mystring)
End Sub

▶ 出力結果

456

⚠️注意

単純な四捨五入ではなく銀行丸めが使われる

VBAのCInt関数は「銀行丸め(バンクラウンド)」を採用しているため、
小数点以下がちょうど0.5の場合は最も近い偶数に丸められます。

銀行丸めとは
四捨五入で「ちょうど .5 のとき」、最も近い偶数に丸める方法です。

結果
CInt(0.5) 0
CInt(1.5) 2
CInt(2.5) 2
CInt(3.5) 4
CInt(4.5) 4
CInt(5.5) 6

単純な四捨五入をしたい場合には、Format関数ワークシート関数のRoundメソッドを使用しましょう。

Sub Sample()
    Debug.Print Format(0.5, 0)
    Debug.Print Format(1.5, 0)
    Debug.Print Format(2.5, 0)
    Debug.Print Format(3.5, 0)
    Debug.Print Format(4.5, 0)
    Debug.Print Format(5.5, 0)
End Sub

▶ 出力結果

1
2
3
4
5
6

変換可能な範囲は-32,768~32,767の整数

CInt関数の戻り値はInteger型(16ビット整数)のため、範囲外の値を渡すとオーバーフローエラーになります。

Sub Sample()
    On Error Resume Next
    Debug.Print CInt(40000)  ' オーバーフローエラー
    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 CInt(Empty)
    Debug.Print CInt(Null)
    If Err.Number <> 0 Then Debug.Print "Null変換時のエラー:" & Err.Description
    On Error GoTo 0
End Sub

▶ 出力結果

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

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

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

▶ 出力結果

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

関連するVBA関数

その他の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?