VBA - 创建空数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43005454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:20:44  来源:igfitidea点击:

VBA - Create empty array

arraysvba

提问by Jonas

I have a function that takes an array of strings and map each string to a Dateinstance. The function boils down to the following code.

我有一个函数,它接受一个字符串数组并将每个字符串映射到一个Date实例。该函数归结为以下代码。

Private Function ParseDates(dates() As String) As Date()
  Dim res() As Date
  Dim i As Integer

  If UBound(dates) >= 0 Then
    ReDim res(UBound(dates)) As Date
  End If


  For i = LBound(dates) To UBound(dates)
    res(i) = #01/01/2000#
  Next i
  ParseDates = res
End Function

The function works just fine as long as the argument datesis nonempty. When datesis empty resis not given a dimension. As a result, the returned value is not enumerable causing to users of this function to crash if the result is enumerated in a loop.

只要参数datesnonempty ,该函数就可以正常工作。当dates为空时res不给定维度。因此,如果在循环中枚举结果,则返回值不可枚举,导致此函数的用户崩溃。

parsedDates = ParseDates(input) 
For i = 1 To UBound(parsedDates) ' Suscription out of range
  ...

How do I instantiate and return an empty array, when dates is empty?

当日期为空时,如何实例化并返回一个空数组?

If you call Split("",".")you receive an object with type String(0 to -1). I need my function to return a object of type Date(0 to -1)as Date()is not an actual array.

如果你打电话,Split("",".")你会收到一个类型为 的对象String(0 to -1)。我需要我的函数返回一个类型的对象,Date(0 to -1)因为Date()它不是一个实际的数组。

I have tried with ReDim res(-1)this causes an Subscript out of rangeerror.

我试过ReDim res(-1)这会导致Subscript out of range错误。

采纳答案by Nathan_Sav

I've used something like this in the past.

我过去使用过类似的东西。

Public Function IS_ARRAY_EMPTY(arrInput As Variant) As Boolean

Dim lngTemp As Long

On Error GoTo eHandle

lngTemp = UBound(arrInput)

IS_ARRAY_EMPTY = False

Exit Function

eHandle:
    IS_ARRAY_EMPTY = True

End Function

回答by Rich Holton

This seems to do the trick:

这似乎可以解决问题:

Private Declare Function EmptyDateArray Lib "oleaut32" Alias "SafeArrayCreateVector" (Optional ByVal vt As VbVarType = vbDate, Optional ByVal lLow As Long = 0, Optional ByVal lCount As Long = 0) As Date()

Function emptyDate() as Date()
    emptyDate = EmptyDateArray()
End Function

Based on an answer by user wgwetofor this questionat VBForums.

基于用户wgweto在 VBForums 上对这个问题的回答。

回答by John Coleman

You specifically mentioned problems in which the calling code needs to iterate over the return value and that iterating over a non-dimensioned array throws an error. Collections don't have that problem. One possibility would be to refactor your code so that it returns a collection (which might or might not have zero elements):

您特别提到了调用代码需要迭代返回值以及迭代无维数组会引发错误的问题。集合没有这个问题。一种可能性是重构您的代码,使其返回一个集合(可能有也可能没有零元素):

Private Function ParseDates(dates() As String) As Collection
  Dim res As New Collection
  Dim i As Long

  For i = LBound(dates) To UBound(dates)
    res.Add #1/1/2000#
  Next i

  Set ParseDates = res
End Function

Say the calling code has the line:

假设调用代码有以下行:

Set C = ParseDates(dates)

Even if C.Count = 0, the following loop works:

即使C.Count = 0,以下循环也有效:

Dim d As Variant

For Each d In C
    'process d
Next d

回答by Moreno

Try this:

尝试这个:

Private Function ParseDates(dates() As String) As Date()
  Dim res() As Date
  Dim i As Integer
  Dim k%
  k=0
  If UBound(dates) >= 0 Then
    ReDim res(UBound(dates)) As Date
  End If


  For i = LBound(dates) To UBound(dates)
    if dates(i)<>"" then
       k=k+1      
    redim preserve res(k)
       end if
res(k) = #01/01/2000#
  Next i
    if k=0 then
     redim res(ubound(dates))
      end if
  ParseDates = res
End Function