我使用下面的功能查找字符串中的6位数字。我需要返回一个包含所有6位数值的数组,但是每当我使用该函数时,我都会得到一个空数组或空值。当我调试代码时,一切似乎都很好(数组包含我想要的值),但在退出函数后,值似乎已经消失了。任何指导将不胜感激。
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
答案 0 :(得分:2)
您的函数返回一个基于0的数组,但您将在1开始分配。
For i = 1 To arrList.Count
arr(i) = arrList(i - 1)
Next i
For i = 0 To arrList.Count - 1
arr(i) = arrList(i)
Next i