简体   繁体   English

Excel VBA中自动筛选的动态嵌套循环

[英]Dynamic Nested Loops for Autofilter in Excel VBA

I have a program setup to spit out a specific summary. 我有一个程序设置来吐出一个特定的摘要。 Now I want to take that program and allow it to accept user inputted ranges. 现在,我要使用该程序,并允许它接受用户输入的范围。 I have it down to 2 levels but it gets conceptually hard for me to go further, though I am using a 3D array to store the values. 我将其降低到2个级别,但是从概念上讲,尽管我正在使用3D数组存储值,但要想进一步深入,就很难了。

Away from my work computer but its something like, 远离我的工作计算机,但它有点像

Dim array1(3) As String 'Fill it with some values like Group1, Group2, Group3...
Dim array2(4) As String 'Again the next level with some values like NY, PA,..., Total
Dim arVal(3,5,4) as Long 'For each Group 1, it will summarize 5 fields for each item in arr2

For i = LBound(array1) To UBound(array1)
   Autofilter based on array1
   For j = LBound(array2) to UBound(array2)
      If Not j = 4
          Autofilter based on array2
      EndIf
      arVal(i, 0, j) = Value
      arVal(i, 1, j) = Value
      ...
   Next j
Next i

One idea I have is to use linked lists, where there will be the 1st list where each subsequent list will chain to each node in the list. 我的一个想法是使用链接列表,其中将有第一个列表,每个后续列表将链接到列表中的每个节点。 Only problem is i only conceptually learned about linked lists in C++ so while this may be a solution I have no clue on how to implement this in VBA. 唯一的问题是我仅在概念上了解了C ++中的链接列表,因此尽管这可能是一个解决方案,但我不知道如何在VBA中实现此功能。

Addition: 加成:

array1 = {Group 1, Group 2, .... Total} array1 = {组1,组2,...。总计}
array2 = {Northeast, West, Southeast....} array2 = {东北,西部,东南....}
arVal(1, 1, 1) = For all people within Group 1 in the Northeast Region the Sum of Potatoes bought(Value 1) arVal(1,1,1)=对于东北地区第1组中的所有人,购买的土豆总和(值1)
arVal(1, 1, 2) = For all people within Group 1 in the Northeast Region the unique count of people(Value 2) arVal(1,1,2)=对于东北地区第1组内的所有人员,唯一的人数(值2)

and then it goes on to 然后继续

arVal(1, 2, 1) = For all people within Group 1 in the WEST region the Sum of Potatoes.... arVal(1,2,1)=对于西部地区第1组中的所有人,马铃薯总和....
and so forth such that for each group, I want to summarize each of the regions within that group. 这样,对于每个组,我想总结一下该组中的每个区域。

The problem arises if I want to dynamically add more subcategories under the regions. 如果我要在区域下动态添加更多子类别,则会出现问题。

So I guess the main question is if given an integer, to create a nested loop equal to that integer such that: 所以我想主要的问题是如果给定一个整数,以创建一个等于该整数的嵌套循环,使得:

If i = 2
{ For Each item in Group
     For Each item in Regions
         some function
}
If i = 3
{ For Each item in Group
     For Each item in Regions
        For Each item in NextSubcategory
           some function
}

Though I could limit the amount of subcategories one could go and use a switch statement, the program would be more robust if dynamic. 尽管我可以限制子类别的数量并使用switch语句,但是如果程序是动态的,则程序会更健壮。 Plus the impending aneurysm if I try to use multidimensional arrays to store the values. 如果我尝试使用多维数组存储值,还要加上即将发生的动脉瘤。

On the topic of Linked lists in VBA this may be useful to you. 在VBA中的“链接列表”主题上,这可能对您有用。

http://www.cpearson.com/excel/classes.aspx http://www.cpearson.com/excel/classes.aspx

You could then follow the methodology you used in C++ to create a linked list using the VBA class. 然后,您可以遵循您在C ++中使用的方法来使用VBA类创建链接列表。

Edit: this may also help Link 编辑:这也可能有助于链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM