Excel VBA分组 - 如何在一个组级别中创建多个组

时间:2016-02-27 00:39:11

标签: excel vba excel-vba

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

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

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

问题:我希望所有分组都属于一个分组级别。因此,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

1 个答案:

答案 0 :(得分: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
相关问题