0

I'm using the function below to find 6 digit numbers in a string. I need to return an array with all 6 digit values, however I keep getting an empty array or empty value whenever I use the function. When I debug the code everything seems fine (the array contains the values I want it to), but after it exits the function the values seem to have disappeared. Any guidance would be appreciated.

Public Function GetMy6Digits(cell As Range)
Dim s As String
Dim i As Integer
Dim answer
Dim counter As Integer

Dim arrList As Object
Set arrList = CreateObject("System.Collections.ArrayList") 'Create the ArrayList

'get cell value
s = cell.Value

'set the counter
counter = 0
'loop through the entire string
For i = 1 To Len(s)
    'check to see if the character is a numeric one
    If IsNumeric(Mid(s, i, 1)) = True Then
        'add it to the answer
        answer = answer + Mid(s, i, 1)
        counter = counter + 1
        'check to see if we have reached 8 digits
        If counter = 6 Then
            Item = answer
            arrList.Add (Item)
            counter = 0
'           Exit Function
        End If
     Else
     'was not numeric so reset counter and answer
     counter = 0
     answer = ""
    End If
Next i

Dim arr() As String
ReDim arr(arrList.Count)

For i = 1 To arrList.Count
    arr(i) = arrList(i - 1)
Next i
GetMy6Digits = arr()

End Function
2
  • How are you calling the function? Commented Nov 18, 2016 at 0:10
  • arr = GetMy6Digits(Worksheet.Range("A" & i)) Commented Nov 18, 2016 at 0:17

1 Answer 1

2

Your function returns a 0 based array but you are starting your assignments at 1.

For i = 1 To arrList.Count
    arr(i) = arrList(i - 1)
Next i

Correction

    For i = 0 To arrList.Count - 1
        arr(i) = arrList(i)
    Next i

Sign up to request clarification or add additional context in comments.

Comments

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.