如何删除自动筛选期间隐藏的行

时间:2018-01-03 06:12:47

标签: vba excel-vba excel

我希望有人可以帮助解决这个问题。我想删除自动筛选器后删除两行。这就是数据在工作表上的显示方式:

col(A)   Col(B)   Col(C)   Col(D)  Col(E)  Col(F)  Col(G)

head1    head2     head3    head4   head5   head6  head7

data     data      data     data     data   data   data  data  data  data   data

                                      total orders old   data sum
                                      total orders new   data sum   

我需要一种方法来删除旧订单和新订单总数。问题是数据范围各不相同。在自动过滤器删除数据行后,以下代码应该删除活动单元格。有什么方法可以在自动过滤器过滤数据行时删除隐藏的行(总订单旧订单和新订单总数)?

    With Sheet18

    'delete the header for the IMGA range
     Dim find As String: find = Me.CboDelete.value
     Dim rng1 As Range


     Set rng1 = Sheets("Jeanette 2017 Monthly Data Sets").Cells.find(What:=find, After:=Sheets("Jeanette 2017 Monthly Data Sets").Cells(1, 1), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)

  If Not rng1 Is Nothing Then
     rng1.Offset(-1).EntireRow.Delete
  End If

'delete the IMGA from the Monthly data set

        'Apply the filter
        .Range("A84:I" & .Rows.count).AutoFilter Field:=1, Criteria1:=Me.CboDelete ', Operator:=xlOr, Criteria2:="="


        With .AutoFilter.Range
            'On Error Resume Next
            Set rng = .Offset(1, 0).Resize(.Rows.count - 1, 2)
                      .SpecialCells (xlCellTypeVisible)
            On Error GoTo 0
            If Not rng Is Nothing Then rng.EntireRow.Delete 'Shift:=xlUp

           End With

'delete the total order and total imc order rows

'.Range("A84:A2000" & .Rows).Activate
Do Until ActiveCell.Offset(1, 0).value <> "IMGA" 'And ActiveCell.value <> ""


If ActiveCell.Offset(2, 0).value = "" Then
'Rows(ActiveCell.Row & ":" & Rows.count).Delete

ActiveCell.Offset(rowOffset:=4, columnOffset:=0).Activate
'MsgBox "what is the position of the active cell"

ActiveCell.Offset(1, 0).Resize(2, 1).EntireRow.Delete

End If

Loop
Exit Sub

1 个答案:

答案 0 :(得分:0)

以下代码解决了我的问题。

If Not Me.CboDelete.value = "IMGA_UNKNOWN" Then

 i = 2 Do Until Sheets("test sheet").Cells(i, 1) = Empty And Sheets("test sheet").Cells(i, 9) = Empty

If Sheets("test sheet).Cells(i, 1) = Me.CboDelete Then

    j = i

    Do Until Sheets("test sheet").Cells(j, 1) = Empty
    j = j + 1
    Loop
   Sheets("test sheet").Rows(i - 1 & ":" & j + 1).Delete Shift:=xlUp

End If

  End If