在vba中不指定嵌套对象的数量
我希望通过ParamArray获取任意数量的对象,然后将它们或嵌套在其中的变量添加到集合中。棘手的部分是,如果嵌套对象是某种容器(集合、脚本字典或甚至是带有count方法的自定义类)也嵌套了变量,我希望它返回集合中的变量,而不是容器中的变量 它是这样的,让我们从创建一个用例开始:在vba中不指定嵌套对象的数量,vba,function,excel,nested-loops,Vba,Function,Excel,Nested Loops,我希望通过ParamArray获取任意数量的对象,然后将它们或嵌套在其中的变量添加到集合中。棘手的部分是,如果嵌套对象是某种容器(集合、脚本字典或甚至是带有count方法的自定义类)也嵌套了变量,我希望它返回集合中的变量,而不是容器中的变量 它是这样的,让我们从创建一个用例开始: Sub MakeItems() Dim ReturnedColl as Collection Dim aString as String Dim TopColl as New Collection, NestedCol
Sub MakeItems()
Dim ReturnedColl as Collection
Dim aString as String
Dim TopColl as New Collection, NestedColl as New Collection, SubNestedDic as New Dictionary
Dim aRangeofManyCells as Range, aRangeofOneCell as Range
Dim anObject as newObject, NestedObject as New Object, SubNestedObject as New Object
aString = "Just a string"
Set aRangeofManyCells = Range("A1:C3")
Set aRangeofOneCell = Range("A4")
SubNestedDic.Add SubNestedObject
SubNestedDic.Add aRangeofOneCell
NestedColl.Add SubNestedDic
NestedColl.Add NestedObject
NestedColl.Add SubNestedDic
NestedColl.Add aRangeofManyCells
TopColl.Add aString
TopColl.AddNestedColl
Set ReturnedColl = UnNest(TopColl, TopColl, anObject, Range("Sheet1:Sheet3!Q1"))
For each Item in ReturnedColl
'do something
Next Item
End Sub
这是我搞不懂的部分。
我想做一个这样的循环,使项目成为新项目,然后查看项目中的每个项目(如果有),但不要忘记原始项目,因为我必须转到下一个项目
Function UnNest(ParamArray Items() as Variant) as Collection
For Each Item in Items
If Item 'is a container of some sort' Then
'some kind of loop through all nests, subnests, subsubnests,...
Else
UnNest.Add Item
Endif
Next Item
End Function
因此,最终结果应该是一个包含以下内容的集合:
aString的“只是一根线”
9个范围对象,对应于aRangeofManyCells中的单元格范围(“A1:C3”)
1范围对象对应于aRangeofOneCell的范围(“A4”)
对象包括一个对象、嵌套对象和子嵌套对象
所有这些都是2x,因为我将TopColl作为函数2x的参数
而且,
一个额外的anObject,因为我将它作为参数添加到函数中
3个范围对象,对应于图纸1Q1、图纸2Q2、图纸3Q3
我知道这是一个很高的要求,但必须有某种方法来完成这个循环。
谢谢你的帮助 此例程似乎可以解决您的一个用例。当然,它对我有效,尽管除了正则变量和数组之外,我没有传递任何东西 我无法克服的一个问题是,我无法确定对象的类型。除非你能解决这个问题,否则我看不出如何实现你的全部目标
Sub DeNestParamArray(RetnValue() As Variant, ParamArray Nested() As Variant)
' Coded Nov 2010
' Each time a ParamArray is passed to a sub-routine, it is nested in a one
' element Variant array. This routine finds the bottom level of the nesting and
' sets RetnValue to the values in the original parameter array so that other routine
' need not be concerned with this complication.
Dim NestedCrnt As Variant
Dim Inx As Integer
NestedCrnt = Nested
' Find bottom level of nesting
Do While True
If VarType(NestedCrnt) < vbArray Then
' Have found a non-array element so must have reached the bottom level
Debug.Assert False ' Should have exited loop at previous level
Exit Do
End If
If NumDim(NestedCrnt) = 1 Then
If LBound(NestedCrnt) = UBound(NestedCrnt) Then
' This is a one element array
If VarType(NestedCrnt(LBound(NestedCrnt))) < vbArray Then
' But it does not contain an array so the user only specified
' one value; a literal or a non-array variable
' This is a valid exit from this loop
Exit Do
End If
NestedCrnt = NestedCrnt(LBound(NestedCrnt))
Else
' This is a one-dimensional, non-nested array
' This is the usual exit from this loop
Exit Do
End If
Else
Debug.Assert False ' This is an array but not a one-dimensional array
Exit Do
End If
Loop
' Have found bottom level array. Save contents in Return array.
ReDim RetnValue(LBound(NestedCrnt) To UBound(NestedCrnt))
For Inx = LBound(NestedCrnt) To UBound(NestedCrnt)
If VarType(NestedCrnt(Inx)) = vbObject Then
Set RetnValue(Inx) = NestedCrnt(Inx)
Else
RetnValue(Inx) = NestedCrnt(Inx)
End If
Next
End Sub
Public Function NumDim(ParamArray TestArray() As Variant) As Integer
' Returns the number of dimensions of TestArray.
' If there is an official way of determining the number of dimensions, I cannot find it.
' This routine tests for dimension 1, 2, 3 and so on until it get a failure.
' By trapping that failure it can determine the last test that did not fail.
' Coded June 2010. Documentation added July 2010.
' * TestArray() is a ParamArray because it allows the passing of arrays of any type.
' * The array to be tested in not TestArray but TestArray(LBound(TestArray)).
' * The routine does not validate that TestArray(LBound(TestArray)) is an array. If
' it is not an array, the routine return 0.
' * The routine does not check for more than one parameter. If the call was
' NumDim(MyArray1, MyArray2), it would ignore MyArray2.
Dim TestDim As Integer
Dim TestResult As Integer
On Error GoTo Finish
TestDim = 1
Do While True
TestResult = LBound(TestArray(LBound(TestArray)), TestDim)
TestDim = TestDim + 1
Loop
Finish:
NumDim = TestDim - 1
End Function
Sub-DeNestParamArray(RetnValue()作为变量,ParamArray嵌套()作为变量)
“2010年11月
'每次将ParamArray传递给子程序时,它都嵌套在一个子程序中
'元素变量数组。此例程查找嵌套的底层,并
'将RetnValue设置为原始参数数组中的值,以便其他例程
“不必担心这种复杂情况。
Dim NestedCrnt作为变体
作为整数的Dim Inx
NestedCrnt=嵌套
'查找嵌套的底部级别
做正确的事
如果VarType(NestedCrnt)