help vba split function cause string byte doubled

ww4612

Well-known Member
Joined
Apr 24, 2014
Messages
515
hello
I have confronted a problem that when i split a string with split function. the number of byte of the splited string has doubled. for example:
Code:
Sub t()
Dim s As String
vv = Split("665", "-")
MsgBox LenB(vv(0))
End Sub
the output from the message is 6. Lenb("665") should return 3 instead of 6. I need to use Lenb function to distinguish DBCS character.
Any solution?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
i think the problem is not associated with split function but Lenb function.
Code:
msgbox lenb("566")
output 6 instead of 3. that is the problem i am facing of.
 
Upvote 0
i think the problem is not associated with split function but Lenb function.
Code:
msgbox lenb("566")
output 6 instead of 3. that is the problem i am facing of.
I know nothing about DBCS, but Len("665") = 3 while LenB("665") = 6 which seems to be the number of bytes required to store "665".

Try this:
Code:
Sub test()
Dim N As Long, D As Double, S As String
N = 665
D = 665
S = "665"
MsgBox  Len(N) & vbNewLine & LenB(N) & vbNewLine & Len(D) &  vbNewLine & LenB(D) & vbNewLine & Len(S) & vbNewLine  & LenB(S)
End Sub
 
Upvote 0
Try this and see
Code:
Sub test()
Dim vv

vv = Split("665", "-")

MsgBox "Len(" & Chr(34) & "665" & Chr(34) & ") = " & Len("665") & vbLf & _
       "LenB(" & Chr(34) & "665" & Chr(34) & ") = " & LenB("665") & vbLf & _
       "Len(vv(0)) = " & Len(vv(0)) & vbLf & _
       "LenB(vv(0)) = " & LenB(vv(0))
End Sub
 
Upvote 0
I know nothing about DBCS, but Len("665") = 3 while LenB("665") = 6 which seems to be the number of bytes required to store "665".

Try this:
Code:
Sub test()
Dim N As Long, D As Double, S As String
N = 665
D = 665
S = "665"
MsgBox  Len(N) & vbNewLine & LenB(N) & vbNewLine & Len(D) &  vbNewLine & LenB(D) & vbNewLine & Len(S) & vbNewLine  & LenB(S)
End Sub

the results is
4
4
8
8
3
6
my motivation to make the code is to determine the type of string. if the type of string is already known, there will not be a need for this function. a standard alphabetic and digital number character should be 1 byte length and a DCBS character should be 2 byte length. my OS seems to store both type of string with 2 byte length.
 
Upvote 0
Try this and see
Code:
Sub test()
Dim vv

vv = Split("665", "-")

MsgBox "Len(" & Chr(34) & "665" & Chr(34) & ") = " & Len("665") & vbLf & _
       "LenB(" & Chr(34) & "665" & Chr(34) & ") = " & LenB("665") & vbLf & _
       "Len(vv(0)) = " & Len(vv(0)) & vbLf & _
       "LenB(vv(0)) = " & LenB(vv(0))
End Sub

this code output
Len("665") = 3
LenB("665") = 6
Len(vv(0)) = 3
LenB(vv(0)) = 6
I suppose it has something to do with my OS language setting
 
Upvote 0
the excel build in function: =lenb("665") output 3. but vba function lenb("665") output 6. I am unable to used worksheetfunction.lenb("665"). otherwise, it can be a solution by using worksheetfunction.lenb.
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,342
Members
448,570
Latest member
rik81h

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top