2016-02-27 81 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

不用擔心!很高興你能解決它:) –