动态嵌套 For 循环
首先,我为开始一个新线程而道歉,但原始线程变得混乱,因为我无法很好地表达我的要求(链接到原始线程:Excel VBA 中自动筛选的动态嵌套循环)。但现在我实际上已经按照我喜欢的方式编写了程序,除了使用 switch 语句而不是更动态地使用嵌套循环。
编辑:
RSum 用于存储范围和布尔值。用户选择列的标题单元格,并选择在汇总时是否要获取该列的总和或唯一计数。这允许这些对象的集合以允许对多个列进行汇总。这个输入对于动态化来说还不错。下一个输入以 rtemp 开头并以 array1 结尾,用户再次选择列的标题单元格,但这会获取该列中的值并将唯一列表保存到 array1。使用此列表,for 循环使用数组的值作为自动过滤器的条件来循环遍历数组。对于自动筛选后循环中的每个步骤,使用 SumThisA 将 RSum 对象集合作为输入来计算摘要。数据按列排列,其中每行都是唯一的记录。
所以问题是,对于下面的代码,我希望用户能够选择要汇总的类别数量,有一个弹出窗口来填写这些范围(可以弄清楚),然后运行一个过滤器,如下所示:
for i = 0 to UBound(array1)
Autofilter criteria1:=array1(i)
for j = 0 to UBound(array2)
Autofilter criteria1:=array2(j)
......
for x = 0 to UBound(arrayx)
Autofilter criteria1:=arrayx(x)
aSum(i,j,....x) = somefunction
现在我知道我需要使用递归函数,但以前从未使用过递归函数,并且该程序有些复杂,这超出了我的理解范围。有人能帮助解释如何在这种情况下使用它吗?另外,由于该程序的通用性,它对许多人来说可能是一个有用的工具。
'---------Initialize Arrays---------------'
t = sMax - 1
Dim aSum()
ReDim aSum(UBound(arr1), t)
'---------------------Perform Summary----------------'
For i = LBound(arr1) To UBound(arr1)
If i = 0 Then
Data.AutoFilter field:=afield, Criteria1:=arr1, Operator:=xlFilterValues
Else
Data.AutoFilter field:=afield, Criteria1:=arr1(i)
End If
temp = SumThisA(SumValues, sMax)
For j = LBound(temp) To UBound(temp)
aSum(i, j) = temp(j)
Next j
Next i
总金额:
1.数组A(1)--------100
- 数组B(1)------30
- 数组B(2)------70
2.数组A(2)--------200
- 数组B(1)-----120
- 数组B(2)------80
3.总计------------300
First off, I apologize for starting a new thread but the original got confusing because I couldn't articulate my ask well (Link to original thread: Dynamic Nested Loops for Autofilter in Excel VBA). But now I have actually written the program to the way I like except using a switch statement instead of the more dynamic use of nested looping.
edit:
RSum is used to store a range and a boolean. The user selects the header cell for a column and chooses whether they want get a summation of that column or a unique count when summarizing. This allows for a collection of these objects to allow summarizing of multiple columns. This input wasn't so bad to make dynamic. The next input which starts as rtemp and ends as array1, is again the user selects the header cell for a column but this it takes the values in that column and saves a unique list to array1. With this list a for loop loops through the array using its value as criteria for an autofilter. For each step in the loop after the autofilter, the summary is calculated using the SumThisA taking the RSum object collection as an input. The data is laid out in columns where each row is a unique record.
So the question is, for the below code, I want the user to be able to select the number of categories to summarize by, have a popup to fill in those ranges (can figure this out), and then run a filter as such:
for i = 0 to UBound(array1)
Autofilter criteria1:=array1(i)
for j = 0 to UBound(array2)
Autofilter criteria1:=array2(j)
......
for x = 0 to UBound(arrayx)
Autofilter criteria1:=arrayx(x)
aSum(i,j,....x) = somefunction
Now I understand I would need to use a recursive function, but having never used one before and the somewhat complexity of this program, it is out of my understanding. Would anyone be able to help explain how to use it in this context? Plus because of the generalization of this program, it could be a useful tool for many people.
'---------Initialize Arrays---------------'
t = sMax - 1
Dim aSum()
ReDim aSum(UBound(arr1), t)
'---------------------Perform Summary----------------'
For i = LBound(arr1) To UBound(arr1)
If i = 0 Then
Data.AutoFilter field:=afield, Criteria1:=arr1, Operator:=xlFilterValues
Else
Data.AutoFilter field:=afield, Criteria1:=arr1(i)
End If
temp = SumThisA(SumValues, sMax)
For j = LBound(temp) To UBound(temp)
aSum(i, j) = temp(j)
Next j
Next i
Sum of Dollars For:
1. arrayA(1)-------100
- arrayB(1)------30
- arrayB(2)------70
2. arrayA(2)-------200
- arrayB(1)-----120
- arrayB(2)------80
3. Total-----------300
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个非常笨拙的递归示例,说明您似乎想做的事情。我伪造了一些标准,所以不要纠结于我如何测试它,重要的是函数 Filter 如何递归运行。如果我能更准确地指出您想要的东西,我就可以更精确地制作它,并且减少硬编码。
测试工具:
递归过滤器功能:
Here's a very kludgy example of recursion for what it seems you want to do. I faked up some criteria, so don't get hung up on how I'm testing for that, what's important is how the function Filter functions recursively. If I could pinpoint more exactly what you wanted I could craft it more precisely, and with less hardcoding.
Test Harness:
Recursive Filter Function:
您是否考虑过使用数据透视表?您的要求似乎非常接近该功能......
Have you considered using a pivot table ? Your requirements seem very close to that functionality...