Очистить содержимое видимых клеток в отфильтрованной колонке - PullRequest
0 голосов
/ 14 июня 2019

Я фильтрую вспомогательную ячейку, чтобы найти ячейки в столбце B, для которых необходимо очистить содержимое.После фильтрации вспомогательной ячейки, которая идентифицировала ячейки в столбце B, для которых необходимо очистить содержимое, у меня возникают проблемы с очисткой содержимого в этой ячейке.

Я получил общее представление, но не могу понять, как очиститьвидимые ячейки, начиная только с первой видимой ячейки до последней видимой ячейки.Моя проблема заключается в определении, где находится начало первой видимой ячейки после применения фильтра и где находится последняя видимая ячейка.

Sub Macro1()
'
' Macro1 Macro

Dim wb As Workbook
Dim ws As Worksheet
Dim FoundCell1 As Range
    Set wb = ActiveWorkbook
    Set ws = ActiveSheet

'This identifying the row of the last cell to filter on
Const WHAT_TO_FIND1 As String = "Tango"
Set FoundCell1 = ws.Range("AX:AX").Find(What:=WHAT_TO_FIND1)


'This is filtering on the helper cell to determine what cells need to be cleared.
ws.Range("$BA$8:$BA$" & FoundCell1.Row).AutoFilter Field:=1, Criteria1:= _
    "Delete"

'This is where I'm having issues.  I would like to replace B2 with a more dynamic code 
'that finds the first visible cell after the filter is applied and start there.  
'I think the xlUp solves the issue of finding the last visible cell but I am not sure 
'if that is the best or correct method.
ws.Range("B2:B" & Rows.Count).End(xlUp).SpecialCells(xlCellTypeVisible).ClearContents
End Sub

1 Ответ

1 голос
/ 14 июня 2019

Вот как бы я это сделал:

Sub tgr()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim FoundCell1 As Range

    Set wb = ActiveWorkbook
    Set ws = wb.ActiveSheet

    'This identifying the row of the last cell to filter on
    Const WHAT_TO_FIND1 As String = "Tango"
    Set FoundCell1 = ws.Range("AX:AX").Find(What:=WHAT_TO_FIND1)
    If FoundCell1 Is Nothing Then Exit Sub  'WHAT_TO_FIND1 not found

    'This is filtering on the helper cell to determine what cells need to be cleared.
    With ws.Range("$BA$8:$BA$" & FoundCell1.Row)
        If .Row < 8 Or .Rows.Count = 1 Then Exit Sub   'No data

        .AutoFilter Field:=1, Criteria1:="Delete"
        On Error Resume Next    'Suppress error in case there are no visible cells
        Intersect(.Worksheet.Columns("B"), .Offset(1).Resize(.Rows.Count - 1).EntireRow).SpecialCells(xlCellTypeVisible).ClearContents
        On Error GoTo 0         'Remove "On Error Resume Next" condition
        .AutoFilter
    End With

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...