VBA function does not work when pasting values ​​into an array

I am completely puzzled by why this does not return a value. This collection is supposed to functionsreturn SUMall ASCII valuesin String. Lines Stringand Number arraysare built correctly, but SUM functionworks with Array, however, when he tries to assign this summed value to the returned variable, he fails.

By “fails”, I mean that the code does not go to the line of the sum, and the function returns #VALUE!to the worksheet. Everything else checks in immediate windowand passes correctly to the last line. Any suggestions are welcome.

Private Function StringToCharArray(ByRef sIn As String) As String()

    StringToCharArray = Split(StrConv(sIn, vbUnicode), Chr(0))
    'provided by Fencliff at http://www.mrexcel.com/forum/excel-questions/342725-split-string-into-array.html

End Function

Function StringToNumber(MyString As String) As Integer

    Dim StringArray() As String
    Dim NumberArray() As Long

    StringArray() = StringToCharArray(MyString)

    ReDim NumberArray(UBound(StringArray))

    For i = LBound(StringArray) To UBound(StringArray)
        NumberArray(i) = Asc(StringArray(i))
    Next i

    StringToNumber = Application.WorksheetFunction.Sum(NumberArray)

End Function
+4
source share
1

"abc", Split "a", "b", "c", "". , . ascii, .

:

For i = LBound(StringArray) To UBound(StringArray) - 1

, ( ) Unicode, .

+4

Source: https://habr.com/ru/post/1541149/


All Articles