Excel VBA分组 – HOWTO一组中的多个组

Excel工作表布局:垂直堆叠10个数据表,每个表之间有一个空行。

目标:将10个表中的空白数据行分组并隐藏(因为并不是每个表中的所有行都会返回数据)。

代码:逐行testing每个表。 一旦它为空白行testingTRUE,它将退出DO LOOP,并将空白行的其余部分隐藏到最后一行。 它移动到下一个表,并testing/隐藏这个表的空行…等。 直到所有的表都经过testing,并且所有的空行被分组/隐藏。

问题:我希望所有分组归入一个分组级别。 所以Excel工作表左上angular的“组级别”将只显示“1,2”作为选项,所有的分组同时隐藏/closures。 运行此代码后,显示8个不同的组级别“1,2,3,4,5,6,7,8”。

我怎样才能让我的代码在一个组别级别上排列所有组别?

'Group Attribute Rollups x = linesheet_rollupatt11_row Do Until x > linesheet_rollupatt125_row If Cells(x, linesheet_brand_clmn) = "" Then att1_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt21_row Do Until x > linesheet_rollupatt225_row If Cells(x, linesheet_brand_clmn) = "" Then att2_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt31_row Do Until x > linesheet_rollupatt325_row If Cells(x, linesheet_brand_clmn) = "" Then att3_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt41_row Do Until x > linesheet_rollupatt425_row If Cells(x, linesheet_brand_clmn) = "" Then att4_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt51_row Do Until x > linesheet_rollupatt525_row If Cells(x, linesheet_brand_clmn) = "" Then att5_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt61_row Do Until x > linesheet_rollupatt625_row If Cells(x, linesheet_brand_clmn) = "" Then att6_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt71_row Do Until x > linesheet_rollupatt725_row If Cells(x, linesheet_brand_clmn) = "" Then att7_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt81_row Do Until x > linesheet_rollupatt825_row If Cells(x, linesheet_brand_clmn) = "" Then att8_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt91_row Do Until x > linesheet_rollupatt925_row If Cells(x, linesheet_brand_clmn) = "" Then att9_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt101_row Do Until x > linesheet_rollupatt1025_row If Cells(x, linesheet_brand_clmn) = "" Then att10_count = x Exit Do End If x = x + 1 Loop x = linesheet_rollupatt111_row Do Until x > linesheet_rollupatt1125_row If Cells(x, linesheet_brand_clmn) = "" Then att11_count = x Exit Do End If x = x + 1 Loop If Cells(linesheet_rollupatt1header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt1header_row & ":" & linesheet_rollupatt125_row).Select Selection.Rows.Group Else Rows(att1_count & ":" & linesheet_rollupatt125_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt2header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt2header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att2_count & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt3header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt3header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att3_count & ":" & linesheet_rollupatt325_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt4header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt4header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att4_count & ":" & linesheet_rollupatt425_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt5header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt5header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att5_count & ":" & linesheet_rollupatt525_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt6header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt6header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att6_count & ":" & linesheet_rollupatt625_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt7header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt7header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att7_count & ":" & linesheet_rollupatt725_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt8header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt8header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att8_count & ":" & linesheet_rollupatt825_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt9header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt9header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att9_count & ":" & linesheet_rollupatt925_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt10header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt10header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att10_count & ":" & linesheet_rollupatt1025_row).Select Selection.Rows.Group End If If Cells(linesheet_rollupatt11header_row, linesheet_vendorname_clmn).Value = "" Then Rows(linesheet_rollupatt11header_row & ":" & linesheet_rollupatt225_row).Select Selection.Rows.Group Else Rows(att11_count & ":" & linesheet_rollupatt1125_row).Select Selection.Rows.Group End If 

这可能是因为你的分组重叠。 假设你的所有数据都在一列中,并且表格之间只有一行空白行,你应该开始一行较低的分组或者结束一行较高的行。

另外,考虑避免select(通过.Select,然后Selection.Rows.Group)。 例子见下面的代码。

要结束更高一排的分组,请更改

 Rows(linesheet_rollupatt1header_row & ":" & linesheet_rollupatt125_row).Select Selection.Rows.Group 

和类似的路线

 Rows(linesheet_rollupatt1header_row & ":" & linesheet_rollupatt125_row - 1).Group 

或者,稍后要开始分组行,请向每个起始行添加“+ 1”,例如

 Rows(linesheet_rollupatt1header_row + 1 & ":" & linesheet_rollupatt125_row).Group 

要么

 Rows(att2_count + 1 & ":" & linesheet_rollupatt225_row).Group