1

I'm learning how to work with VBA. I need to read an Integer and store it in an Array breaking it down to single characters. For example, if I input the number 1927, I need it to be stored as

    Array[0] = 1
    Array[1] = 9
    Array[2] = 2
    Array[3] = 7

I've been looking how to do this for 2 days.Can someone help ?

1
  • How would you do it manually on a paper? Consider the Array as a number of drawers, one on top of the other, each one labeld with an sequential number starting from zero. Commented Mar 12, 2018 at 14:43

5 Answers 5

3

I find a conversion to a unicode string and subsequent split on Chr(0) is an efficient way to create an array of individual characters.

Dim str As String, arr As Variant, i as long
str = StrConv(CStr(1927), vbUnicode)
arr = Split(str, Chr(0))
redim preserve arr(ubound(arr)-1)
For i = LBound(arr) To UBound(arr)
    Debug.Print arr(i)
Next i

Debug.Print Join(arr, vbNullString)
Sign up to request clarification or add additional context in comments.

1 Comment

That is neat, so long as there are no wide characters.
2

You can convert the integer to a string as follows :

cstr(int)

then you can extract your single numbers as char from the string that you created as follows :

Mid(s, index, 1)

Check this link for more documentation.

Comments

2

This is a possible solution:

Option Explicit

Public Sub SplitIntToArray()

    Dim inputString As String
    inputString = "1927"

    Dim cnt         As Long
    Dim myArr       As Variant
    ReDim myArr(Len(inputString) - 1)

    For cnt = LBound(myArr) To UBound(myArr)
        myArr(cnt) = Mid(inputString, cnt + 1, 1)
    Next cnt        

    For cnt = LBound(myArr) To UBound(myArr)
        Debug.Print myArr(cnt)
    Next cnt

End Sub

It takes the inputString and based on its length it dimensionzes the myArr. Then it loops the newly dimensionized array and it assigns Mid(inputString, cnt+1,1) to every part of the array. THe cnt+1 is needed, because the first char of the string needs to be in the 0th element of the array, as the arrays start at zero.


You can also make a function, taking string and returning array:

Public Sub SplitIntToArray()

    Dim inputString As String
    Dim cnt         As Long
    Dim myArr       As Variant

    myArr = stringToArray("1927")

    For cnt = LBound(myArr) To UBound(myArr)
        Debug.Print myArr(cnt)
    Next cnt

End Sub

Public Function stringToArray(inputString As String) As Variant

    Dim cnt         As Long
    Dim returnArray As Variant

    If Len(inputString) = 0 Then
        stringToArray = Array()
        Exit Function
    End If

    ReDim returnArray(Len(inputString) - 1)

    For cnt = LBound(returnArray) To UBound(returnArray)
        returnArray(cnt) = Mid(inputString, cnt + 1, 1)
    Next cnt

    stringToArray = returnArray

End Function

3 Comments

I am slowing down. the second time today I was beat by seconds.
After lunch I am with double powers, @ScottCraner :)
Obrigada pela ajuda, Vityata!
2

Here's another solution that doesn't require looping to assign the values to an array, or from an array to a string.

Sub Ex()
    Dim Integers() As Byte
    Dim i          As Long
    Integers = StrConv("0123456789", vbFromUnicode)

    'Print the values out, already in an array
    For i = LBound(Integers) To UBound(Integers)
        Debug.Print Chr$(Integers(i))
    Next

    'Put back into a string
    Dim MyInts As String
    MyInts = StrConv(Integers, vbUnicode)
    Debug.Print MyInts
End Sub

1 Comment

StrConv is an interesting built-in function. You pass empty String and it converts it to an array without a problem. You pass empty Array and it throws an error. I see double standards here :)
1

No one appears to have used the mathematical route yet. So let me do that.

Sub numArr()
    Dim num As Integer
    Dim arr() As Integer
    num = 1927
    i = 0

    Do
        i = i + 1
        ReDim Preserve arr(i)
        arr(i) = num Mod 10
        num = num \ 10
    Loop While num > 0

End Sub

This one was more fun writing.

3 Comments

There's an issue. The last digit appears first. Use ReDim arr(0 To Fix(Log(num) / Log(10))) to set the size and For i = UBound(arr) To 0 Step -1 to iterate.
Yes, I left the code to reverse the array on purpose, as there are many easy ways to do that. Also it is not required per se, since the array can be iterated in reverse too. P.S. I did like the ReDim arr(0 To Fix(Log(num) / Log(10))) suggestion.
This is actually perfect! As it turns out, storring backwards would be better for me! Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.