I would like to create a function that will take as a parameter a Cell
and return an Integer
. If the conversion is not successful it should simply return 0, without throwing error messages to the user.
I don't care about the cells that contain float values, they could also return 0 since it doesn't look like integer. But text values like 00001234
should return 1234
and 12 34
should return 0.
How about this:
Option Explicit
Public Function ConvertToInteger(Cell As Range) As Integer
On Error GoTo NOT_AN_INTEGER
ConvertToInteger = CInt(Cell.Value)
Exit Function
NOT_AN_INTEGER:
ConvertToInteger = 0
End Function
Note, for example, that a value of 5.6 will return 6, though. If you want it to be 0 instead, you must check for it and do accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With