Zoeken…


Opmerkingen

VBA zal impliciet sommige types naar string converteren als dat nodig is en zonder extra werk van de kant van de programmeur, maar VBA biedt ook een aantal expliciete stringconversiefuncties, en je kunt er ook zelf een schrijven.

Drie van de meest gebruikte functies zijn CStr , Format en StrConv .

Gebruik CStr om een numeriek type om te zetten in een tekenreeks

Const zipCode As Long = 10012
Dim zipCodeText As String
'Convert the zipCode number to a string of digit characters
zipCodeText = CStr(zipCode)
'zipCodeText = "10012"

Gebruik Opmaak om een numeriek type als een tekenreeks te converteren en op te maken

Const zipCode As long = 10012
Dim zeroPaddedNumber As String
zeroPaddedZipCode = Format(zipCode, "00000000")
'zeroPaddedNumber = "00010012"

Gebruik StrConv om een byte-array van single-byte tekens om te zetten in een string

'Declare an array of bytes, assign single-byte character codes, and convert to a string
Dim singleByteChars(4) As Byte
singleByteChars(0) = 72
singleByteChars(1) = 101
singleByteChars(2) = 108
singleByteChars(3) = 108
singleByteChars(4) = 111
Dim stringFromSingleByteChars As String
stringFromSingleByteChars = StrConv(singleByteChars, vbUnicode)
'stringFromSingleByteChars = "Hello"

Converteer impliciet een bytearray van multi-byte-tekens naar een string

'Declare an array of bytes, assign multi-byte character codes, and convert to a string
Dim multiByteChars(9) As Byte
multiByteChars(0) = 87
multiByteChars(1) = 0
multiByteChars(2) = 111
multiByteChars(3) = 0
multiByteChars(4) = 114
multiByteChars(5) = 0
multiByteChars(6) = 108
multiByteChars(7) = 0
multiByteChars(8) = 100
multiByteChars(9) = 0
            
Dim stringFromMultiByteChars As String
stringFromMultiByteChars = multiByteChars
'stringFromMultiByteChars = "World"


Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow