vba convert string to integer code example

Example 1: vba string to integer

MsgBox CInt("7,55")     ' Integer   -> 8
MsgBox CLng("7,55")     ' Long      -> 8
MsgBox CDbl("7,55")     ' Double    -> 7,55

Example 2: excel vba right word from Long Integer

'If n is a 4-byte Long Integer, the Low (Left) Word is:
If n And &H8000& Then
  	LoWord = n Or &HFFFF0000
Else
    LoWord = n And &HFFFF&
End If

'If n is a 4-byte Long Integer, the High (Right) Word is:
HiWord = (n And &HFFFF0000) \ &H10000

Example 3: excel vba convert string to a number if string is a number

MsgBox Val(str)

'If string does not start with a numeric text value, Val() returns a zero.

Example 4: vba make integer

Public Function MakeInteger%(LoByte As Byte, HiByte As Byte)
  If HiByte And &H80 Then
    MakeInteger = ((HiByte * &H100&) Or LoByte) Or &HFFFF0000
  Else
    MakeInteger = (HiByte * &H100) Or LoByte
  End If
End Function