2016-02-27 24 views
1

Excel工作表布局: 10个垂直堆叠的数据表,每个表之间有一个空行。Excel VBA分组 - HOWTO一组中的多个组

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

代码:逐个测试每个表。一旦它为空白行测试TRUE,它将退出DO LOOP并将该单独表格的其余部分从空白行隐藏到结束行。它移动到下一个表并测试/隐藏该表的空行...等。直到所有表已经过测试并且所有空白行都被分组/隐藏。

期:我希望所有分组属于一个分组级别。因此,Excel工作表左上角的“组级别”将仅显示“1,2”作为选项,并且所有分组同时隐藏/关闭。运行此代码后,它显示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 

回答

0

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

此外,请考虑避免选择(通过.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 
+0

感谢您的建议 - 但我不相信他们实际上是重叠的,这就是为什么我很困惑。例如,每个表格有25行。它测试每个表的行1-25(不测试每个表之间的空行)。在上面的代码中,你会看到每个表都有一个疯狂的变量数量(例如:linesheet_rollupatt1header_row,linesheet_rollupatt2header_row ...)它只测试表格的最后一行的第一行,然后移动到下一个表格。至于冷凝“.group”,我不确定为什么我没有这样做的第一个地方!哈哈哎呀! –

+0

哦,我的天啊!我是一个虚拟的...当我复制并粘贴额外10个表的代码时,我忘记回到我的公共变量中,并定义其余的新变量... –

+0

不用担心!很高兴你能解决它:) –