首页 > 解决方案 > 分组其他列excel后分组消失-无法使用摘要行

问题描述

我正在尝试对 Excel 中的列进行分组。如下面显示的屏幕截图所示,我在第一个屏幕截图中将 C27 分组到 C30。但是,当我转到 E 列并尝试将 E31 分组到 E35 时,它不起作用,并且之前的分组消失了。我怎样才能解决这个问题?我不想添加摘要行,因为没有什么要总结的。

此代码的功能是每隔一列对空白进行分组,例如 C 列中的第 27 到 30 行和 E 列中的第 31 到 25 行

在对列 E 中的第 31 到 34 行进行分组之前

在此处输入图像描述

For iColumn = 1 To 11 Step 2
    'loop through rows
    
    
    iStartRow = -999
    
    'from first cell in column

    For iRow = wksSupervisor.Cells(1, iColumn).End(xlDown).Row + 1 To wksSupervisor.Cells.SpecialCells(xlCellTypeLastCell).Row
    
        iLastRow = wksSupervisor.Cells(10000, iColumn + 2).End(xlUp).Row
        
        'Check if empty cell has text above
        If IsEmpty(wksSupervisor.Cells(iRow, iColumn)) And Not IsEmpty(wksSupervisor.Cells(iRow - 1, iColumn)) Then
            
            'start row of grouping is current row
            iStartRow = iRow
        
        End If
        
        'check if empty cell has text below
        If IsEmpty(wksSupervisor.Cells(iRow, iColumn)) And Not IsEmpty(wksSupervisor.Cells(iRow + 1, iColumn)) Then
            
            'empty cell
            bFoundLastRow = True
            iEndRow = iRow
            
        End If
        
        
        'Check if we have both the start row to group and last row to group
        If (bFoundLastRow = True Or iRow = iLastRow) And (iStartRow <> iEndRow) And (iStartRow <> -999) Then
            iEndRow = iRow
            If IsEmpty(wksSupervisor.Cells(iEndRow, iColumn)) Then
                wksSupervisor.Rows(iStartRow & ":" & iEndRow).Group
                bFoundLastRow = False
            End If
            
        End If
        
        'Create a separate condition grouping 1 row (syntax is different)
        If (bFoundLastRow = True Or iRow = iLastRow) And (iStartRow = iEndRow) Then
                wksSupervisor.Cells(iRow, iColumn).Select
                Selection.Rows.Group
                bFoundLastRow = False
            
        End If
        
    Next iRow

Next iColumn

标签: excelvbagrouping

解决方案


推荐阅读