在VBA中声明长度为0的字符串数组-不可能吗? [英] Declare a 0-Length String Array in VBA - Impossible?

查看:223
本文介绍了在VBA中声明长度为0的字符串数组-不可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的不可能在VBA中声明一个长度为0的数组吗?如果我尝试这样做:

Is it really not possible to declare a 0-length array in VBA? If I try this:

Dim lStringArr(-1) As String

我收到一个编译错误,说range没有值.如果我试图欺骗编译器并在运行时像这样重做:

I get a compile error saying range has no values. If I try to trick the compiler and redim at runtime like this:

ReDim lStringArr(-1)

我收到一个下标超出范围的错误.

I get a subscript out of range error.

我略微改变了上面的内容,但是没有运气.

I've varied the above around a bit but with no luck e.g.

Dim lStringArr(0 To -1) As String

用例

我想将变量数组转换为字符串数组.变体数组可能是空的,因为它来自字典的Keys属性. keys属性会返回一组变体.我希望在代码中使用字符串数组,因为我有一些函数可以处理要使用的字符串数组.这是我正在使用的转换函数.由于lMaxIndex = -1,这将导致下标超出范围错误:

Use Case

I want to convert a variant array to a string array. The variant array may be empty as it comes from the Keys property of a dictionary. The keys property gives back an array of variants. I want an array of strings to use in my code, as I have some functions for processing string arrays I'd like to use. Here's the conversion function I'm using. This throws a subscript out of range error due to lMaxIndex being = -1:

Public Function mVariantArrayToStringArray(pVariants() As Variant) As String()
    Dim lStringArr() As String
    Dim lMaxIndex As Long, lMinIndex As Long
    lMaxIndex = UBound(pVariants)
    lMinIndex = LBound(pVariants)
    ReDim lStringArr(lMaxIndex)
    Dim lVal As Variant
    Dim lIndex As Long
    For lIndex = lMinIndex To lMaxIndex
        lStringArr(lIndex) = pVariants(lIndex)
    Next
    mVariantArrayToStringArray = lStringArr
End Function

Hack

返回包含一个空字符串的单例数组.注意-这不是我们想要的.我们想要一个空数组-这样遍历它就像什么都不做.但是包含空字符串的单例数组通常可以工作,例如如果我们以后想将所有字符串连接到字符串数组中.

Hack

Return a singleton array containing an empty string. Note- this isn't what we want. We want an empty array- such that looping over it is like doing nothing. But a singleton array containing an empty string will often work e.g. if we later want to join all the strings together in the string array.

Public Function mVariantArrayToStringArray(pVariants() As Variant) As String()
    Dim lStringArr() As String
    Dim lMaxIndex As Long, lMinIndex As Long
    lMaxIndex = UBound(pVariants)
    lMinIndex = LBound(pVariants)
    If lMaxIndex < 0 Then
        ReDim lStringArr(1)
        lStringArr(1) = ""
    Else
        ReDim lStringArr(lMaxIndex)
    End If
    Dim lVal As Variant
    Dim lIndex As Long
    For lIndex = lMinIndex To lMaxIndex
        lStringArr(lIndex) = pVariants(lIndex)
    Next
    mVariantArrayToStringArray = lStringArr
End Function

从回答开始更新

这是我用来将变量数组转换为字符串数组的函数. Comintern的解决方案似乎更高级,更通用,如果我仍然坚持使用VBA进行编码,那么我可能会切换到这一天:

Update since answer

Here is the function I'm using for converting a variant array to a string array. Comintern's solution seems more advanced and general, and I may switch to that one day if I'm still stuck coding in VBA:

Public Function mVariantArrayToStringArray(pVariants() As Variant) As String()
    Dim lStringArr() As String
    Dim lMaxIndex As Long, lMinIndex As Long
    lMaxIndex = UBound(pVariants)
    lMinIndex = LBound(pVariants)
    If lMaxIndex < 0 Then
        mVariantArrayToStringArray = Split(vbNullString)
    Else
        ReDim lStringArr(lMaxIndex)
    End If
    Dim lVal As Variant
    Dim lIndex As Long
    For lIndex = lMinIndex To lMaxIndex
        lStringArr(lIndex) = pVariants(lIndex)
    Next
    mVariantArrayToStringArray = lStringArr
End Function

注释

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆