seb*_*anc 1 arrays vba function
我有以下代码:
Function TruncateString(str, n)
' Returns an array with strings no more than n char long, truncated at spaces
Dim truncatedArr() As String
If str <> "" Then
str = remove_spaces_left(str)
For i = 0 To (CLng(Len(str) / n))
Index = InStrRev(Left(str, n), " ")
ReDim Preserve truncatedArr(i)
truncatedArr(i) = Left(str, Index)
If Right(truncatedArr(i), 1) = " " Then truncatedArr(i) = Left(truncatedArr(i), Len(truncatedArr(i)) - 1)
str = Right(str, Len(str) - Index)
Next i
End If
TruncateString = truncatedArr
End Function
Run Code Online (Sandbox Code Playgroud)
我的问题是当函数str为空时返回的值是多少?我有类型兼容性问题
arr = TruncateString (text,15)
arr的定义如下:
dim arr() as string
如果答案需要更多信息,请告诉我.谢谢
您的代码中有几个问题:
Option Explicit在模块的开头使用,以便您将被迫声明所有变量(包括i和Index,应该重命名,因为它会引发与属性的冲突)最后,为了回答你的问题(但我想知道你为什么不自己检查它),你的函数返回一个空数组(存在但从未ReDim编辑).你甚至不能UBound这样的阵列.