first, the value
translates to
String Functions str, CSTRThe STR function converts a numeric value to a string, which returns a string representing a numeric value whose syntax is:
Str(number)The CStr function converts a numeric expression to a string with the following syntax:
CStr(mynumberexpression)
Note:When a value is converted to a string, it is always preceded by a vacancy to indicate positive or negative, that is, the first digit of the string must be a space or a positive sign, and STR will be a valid decimal point.
Demo Code:
Sub Strtranfomationdemo () Dim mydouble as Double mydouble = 234.823 Debug.Print "str:<" & Str ( 24.32) & ">" Debug.Print "str:<" & Str ( -24.32) & ">" Debug.Print "cstr:<" & CStr (Mydoubl e) & ">" End Sub
Run Results
str:< 24.32>str:<-24.32>cstr:<234.823>
Second, the character of the numerical conversion to the numerical data Val function can convert the character data containing the numeric value into double (double) numeric data type, whose syntax is:
Val (String)
Demo Code 1:
Sub showformatval () Dim num as Double, str as String str = Format (now, "short time") num = Val (str) ' If the current time is 4:31 PM, the following statement shows the result: 16:31 Debug.Print str, num End Sub
Demo Code 2:
Sub Transformstr2int ()
Dim num as Double, str as String, tmp as String
' The data in cell A1 is 0.8051
str = thisworkbook.worksheets ("Sheet1"). Range ("A1")
MsgBox "str:" & STR
TMP = Right (str, Len (str)-1)
MsgBox "tmp:" & TMP
num = Val (tmp) + 1 ' string to number
MsgBox "num:" & num
MsgBox hex (num) ' decimal conversion to hexadecimal number
End Sub
Application
- A character value cannot be arithmetic, and can only be used after it has been converted by the Val function.
- You can convert 8, 16, and so on to 10 binary numbers, such as Val (&o77) (8 binary), Val (&hFF) (16 binary), respectively, into decimal 63 and 255
VBA learns to convert between string and numeric strings---