excel vba convert string to number code example
Example 1: excel-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
Example 2: excel vba binary string to long integer
'Fast VBA function to convert binary string to a Long Integer:
Function BitsToLong&(bits$)
Dim i&
Static b() As Byte
If LenB(bits) > 64 Then Exit Function
If LenB(bits) = 64 Then
b = bits
Else
b = String$(32 - Len(bits), "0") & bits
End If
For i = 2 To 62 Step 2
BitsToLong = 2 * BitsToLong Or (b(i) Xor 48)
Next
If (b(0) Xor 48) Then BitsToLong = BitsToLong Or &H80000000
End Function
'-----------------------------------------------------------------------------
MsgBox BitsToLong("1") '<--displays: 1
MsgBox BitsToLong("10") '<--displays: 2
MsgBox BitsToLong("0110") '<--displays: 6
MsgBox BitsToLong("0100101") '<--displays: 37
MsgBox BitsToLong("100000000000000000000") '<--displays: 1048576
MsgBox BitsToLong("11111111111111111111111111111111") '<--displays: -1
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.