Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Redim保留和应用程序出现VBA错误。转置更改数组基_Excel_Vba - Fatal编程技术网

Excel Redim保留和应用程序出现VBA错误。转置更改数组基

Excel Redim保留和应用程序出现VBA错误。转置更改数组基,excel,vba,Excel,Vba,我有一个二维数组。 我想在它的第一个维度上保留一个redim,但我知道redim prese只在最后一个维度上有效。 我尝试使用转置函数,但转置似乎正在将我的数组从基0更改为基1,这正常吗?如果是,如何将其设置回基数0?或者如何解决在更改数组的第一维度时保留数组的问题? 我想向数组中添加2个元素 下面是我的代码的一部分,我发现了这个问题: table3 = Application.Transpose(table3) ReDim Preserve table3(Ubound(tab

我有一个二维数组。 我想在它的第一个维度上保留一个redim,但我知道redim prese只在最后一个维度上有效。 我尝试使用转置函数,但转置似乎正在将我的数组从基0更改为基1,这正常吗?如果是,如何将其设置回基数0?或者如何解决在更改数组的第一维度时保留数组的问题? 我想向数组中添加2个元素

下面是我的代码的一部分,我发现了这个问题:

    table3 = Application.Transpose(table3)
    ReDim Preserve table3(Ubound(table3,1), Ubound(table3,2) +2)
    table3 = Application.Transpose(table3)
我注意到在转置之前,数组是以0为基数,转置之后是以1为基数,我认为这是主要问题。我不想将基数从0更改为1,因为我在代码的其他地方使用了相同的数组,我不想更改整个代码

下面的代码行将给我一个错误

“下标超出范围”

ReDim Preserve table3(Ubound(table3,1), Ubound(table3,2) +2) 
如果我按以下行更改它

ReDim Preserve table3(1 To UBound(table3, 1), UBound(table3, 2) + 2)
它会工作,但我的数组将成为一个基于1的数组,这不是我想要的,我想保持我的索引从0开始,而不是从1开始

转置前

转置后


也许这会帮助您:

'********************************************************************************************************************
' To re-dimension the first dimension of a two-dimension array without getting Excel errors
' Also possible to re-dimension the second dimension
' Usage: myArray = reDimPreserve(myArray, UBound(myArray, 1) + x, UBound(myArray, 2) + y)
' Where x and y are the increments to get to the desired new dimensions
' Returns an empty array if there was an error
'********************************************************************************************************************
Public Function reDimPreserve(ByVal aArray As Variant, ByVal newFirstUBound As Long, ByVal newLastUBound As Long) As Variant
Dim tmpArr As Variant, nOldFirstUBound As Long, nOldLastUBound As Long, nFirst As Long, nLast As Long

If Not IsArray(aArray) Then
    reDimPreserve = Array(Empty)
ElseIf newFirstUBound < UBound(aArray, 1) Or newLastUBound < UBound(aArray, 2) Then
    reDimPreserve = Array(Empty)
Else
    ReDim tmpArr(newFirstUBound, newLastUBound)
    nOldFirstUBound = UBound(aArray, 1)
    nOldLastUBound = UBound(aArray, 2)
    For nFirst = LBound(aArray, 1) To newFirstUBound
        For nLast = LBound(aArray, 2) To newLastUBound
            If nOldFirstUBound >= nFirst And nOldLastUBound >= nLast Then
                tmpArr(nFirst, nLast) = aArray(nFirst, nLast)
            End If
        Next nLast
    Next nFirst
    reDimPreserve = tmpArr
    Erase tmpArr
End If

End Function
'********************************************************************************************************************
'在不出现Excel错误的情况下重新对二维数组的第一个维度进行尺寸标注
'还可以重新标注第二个维度
'用法:myArray=redimprefer(myArray,UBound(myArray,1)+x,UBound(myArray,2)+y)
'其中x和y是达到所需新尺寸的增量
'如果出现错误,则返回空数组
'********************************************************************************************************************
公共函数reDimPreserve(ByVal aArray作为变体,ByVal newFirstUBound作为Long,ByVal newLastUBound作为Long)作为变体
变暗tmpArr作为变型,NOLDFIRSTUBOND作为长型,nOldLastUBound作为长型,nFirst作为长型,nLast作为长型
如果不是IsArray(aArray),那么
redimprefer=数组(空)
否则,如果newFirstUBound=nFirst和nOldLastUBound>=nLast,则
tmpArr(nFirst,nLast)=aArray(nFirst,nLast)
如果结束
下一个nLast
下一个第一
redimprefer=tmpArr
删除tmpArr
如果结束
端函数

也许这会帮助您:

'********************************************************************************************************************
' To re-dimension the first dimension of a two-dimension array without getting Excel errors
' Also possible to re-dimension the second dimension
' Usage: myArray = reDimPreserve(myArray, UBound(myArray, 1) + x, UBound(myArray, 2) + y)
' Where x and y are the increments to get to the desired new dimensions
' Returns an empty array if there was an error
'********************************************************************************************************************
Public Function reDimPreserve(ByVal aArray As Variant, ByVal newFirstUBound As Long, ByVal newLastUBound As Long) As Variant
Dim tmpArr As Variant, nOldFirstUBound As Long, nOldLastUBound As Long, nFirst As Long, nLast As Long

If Not IsArray(aArray) Then
    reDimPreserve = Array(Empty)
ElseIf newFirstUBound < UBound(aArray, 1) Or newLastUBound < UBound(aArray, 2) Then
    reDimPreserve = Array(Empty)
Else
    ReDim tmpArr(newFirstUBound, newLastUBound)
    nOldFirstUBound = UBound(aArray, 1)
    nOldLastUBound = UBound(aArray, 2)
    For nFirst = LBound(aArray, 1) To newFirstUBound
        For nLast = LBound(aArray, 2) To newLastUBound
            If nOldFirstUBound >= nFirst And nOldLastUBound >= nLast Then
                tmpArr(nFirst, nLast) = aArray(nFirst, nLast)
            End If
        Next nLast
    Next nFirst
    reDimPreserve = tmpArr
    Erase tmpArr
End If

End Function
'********************************************************************************************************************
'在不出现Excel错误的情况下重新对二维数组的第一个维度进行尺寸标注
'还可以重新标注第二个维度
'用法:myArray=redimprefer(myArray,UBound(myArray,1)+x,UBound(myArray,2)+y)
'其中x和y是达到所需新尺寸的增量
'如果出现错误,则返回空数组
'********************************************************************************************************************
公共函数reDimPreserve(ByVal aArray作为变体,ByVal newFirstUBound作为Long,ByVal newLastUBound作为Long)作为变体
变暗tmpArr作为变型,NOLDFIRSTUBOND作为长型,nOldLastUBound作为长型,nFirst作为长型,nLast作为长型
如果不是IsArray(aArray),那么
redimprefer=数组(空)
否则,如果newFirstUBound=nFirst和nOldLastUBound>=nLast,则
tmpArr(nFirst,nLast)=aArray(nFirst,nLast)
如果结束
下一个nLast
下一个第一
redimprefer=tmpArr
删除tmpArr
如果结束
端函数

ReDim Preserve table 3(0到UBound(table 3,1),UBound(table 3,2)+1)
我在发布之前已经尝试过了,但失败了。我还尝试了0到ubound(表3,1)-1不起作用。您可以编写一个小的实用函数来转置数组,而不更改其下限。。。您不需要使用
Application.Transpose
或编写函数来直接执行大小调整,而无需任何转置@user11217663-转置确实会更改边界(如果原始下限不是1)输出下限始终是1
table3
如何声明和填充?
ReDim Preserve table3(0到UBound(table3,1),UBound(table3,2)+1)
我在发布之前已经尝试过了,但失败了。我还尝试了0到ubound(表3,1)-1不起作用。您可以编写一个小的实用函数来转置数组,而不更改其下限。。。您不需要使用
Application.Transpose
或编写函数来直接执行大小调整,而无需任何转置@user11217663-转置确实会更改边界(如果原始下限不是1)输出下限始终是1
表3
是如何声明和填充的?非常感谢您的函数完美地工作并完成了工作。非常感谢您的函数完美地工作并完成了工作。