Fonction VBA : VarType
La fonction VBA VarType renvoie un nombre entier correspondant au type de la variable passée en argument.
Utilisation :
VarType(variable)
Les valeurs de retour
La liste des valeurs retournées par la fonction VarType :
| Constante | Valeur |
|---|---|
| vbEmpty | 0 |
| vbNull | 1 |
| vbInteger | 2 |
| vbLong | 3 |
| vbSingle | 4 |
| vbDouble | 5 |
| vbCurrency | 6 |
| vbDate | 7 |
| vbString | 8 |
| vbObject | 9 |
| vbError | 10 |
| vbBoolean | 11 |
| vbVariant | 12 |
| vbDataObject | 13 |
| vbDecimal | 14 |
| vbByte | 17 |
| vbLongLong | 20 |
| vbUserDefinedType | 36 |
| vbArray | 8192 |
Si la variable est un tableau, le nombre renvoyé par la fonction correspondra à l'addition de 2 valeurs. Par exemple, une valeur de 8194 correspond à 8192 (vbArray) + 2 (vbInteger).
Exemple d'utilisation
Utilisation de la fonction VarType pour afficher le type de la variable :
Sub exemple()
variable = "Excel"
MsgBox VarType(variable) 'Renvoie : 8
End Sub
Utilisation de la fonction VarType pour afficher le type du tableau :
Sub exemple()
tableau = Split("x l p", " ")
MsgBox VarType(tableau) 'Renvoie : 8200
End Sub
La valeur 8200 correspond ici à 8192 (vbArray) + 8 (vbString).