Fonctions VBA : CInt et CLng
Les fonctions VBA CInt et CLng convertissent une valeur numérique en nombre entier en arrondissant à l'entier le plus proche.
Utilisation :
CInt(valeur)
CLng(valeur)
Exemple d'utilisation
Utilisation de la fonction CInt pour convertir différents types de valeurs numériques :
Sub exemple()
MsgBox CInt(1) 'Renvoie : 1
MsgBox CInt(-1) 'Renvoie : -1
MsgBox CInt(2.9) 'Renvoie : 3
MsgBox CInt(-2.9) 'Renvoie : -3
MsgBox CInt(4.5) 'Renvoie : 4
MsgBox CInt(-4.5) 'Renvoie : -4
MsgBox CInt(0.424) 'Renvoie : 0
MsgBox CInt("13") 'Renvoie : 13
MsgBox CInt("-4.5") 'Renvoie : -4
MsgBox CInt(True) 'Renvoie : -1
MsgBox CInt(False) 'Renvoie : 0
End Sub
Si le nombre à convertir est trop grand pour être convertit en Integer, convertissez-le en Long à l'aide de la fonction CLng :
Sub exemple()
MsgBox CLng(123456.789) 'Renvoie : 123457
End Sub