VBA learns to convert between string and numeric strings---

Source: Internet
Author: User
Tags string to number

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(mynumberexpressionNote: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---

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.