简体   繁体   English

清除过滤柱中可见细胞的内容

[英]Clear Contents of Visible Cells in Filtered Column

I'm filtering on a helper cell to locate cells in column B that need the contents cleared. 我正在筛选一个辅助单元,以查找B列中需要清除其内容的单元。 Once I filter on the helper cell that has identified cells in column B that need contents cleared, I am having issues clearing the contents in that cell. 一旦我筛选了在B列中标识了需要清除内容的单元格的辅助单元格,我在清除该单元格中的内容时就会遇到问题。

I got the general idea down except I cannot figure out how to clear the visible cells only starting from the first visible cell down to the last visible cell. 我了解了总体思路,只是我无法弄清楚如何仅从第一个可见单元开始到最后一个可见单元清除可见单元。 My issue is identifying where is the start of the first visible cell after the filter is applied and where is the last visible cell. 我的问题是确定应用过滤器后第一个可见单元格的开始位置以及最后一个可见单元格在哪里。

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

Here's how I'd do it: 这是我的处理方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用列引用清除VBA中的单元格内容 - Clear contents of cells in VBA using column reference VBA:将可见的单元格(已过滤)粘贴到相同的行中,但粘贴到另一列中 - VBA: Paste visible cells (filtered) into same rows but another column 从过滤表的一列复制/粘贴/计算可见单元格 - Copy/Paste/Calculate Visible Cells from One Column of a Filtered Table Select 前 800 个可见单元格仅形成一列,即使有超过 800 个可见过滤单元格 - Select first 800 visible cells only form a column, even if there are more then 800 visible filtered cells 清除单元格内容 - Clear the contents of cells EPPLUS清除一系列细胞的内容 - EPPLUS To Clear Contents of A Range Of Cells 清除VBA中具有特定内容的单元格 - Clear cells with specific contents in VBA 当Excel列由按钮过滤时,我的行应更新。 更新是关于将值插入可见的单元格 - When a excel column is filtered by a button, my rows should be updated. The update is about inserting values into visible cells Excel VBA仅连接已过滤列的可见单元格。 包含测试代码 - Excel VBA Concatenate only visible cells of filtered column. Test code included 如何在考虑空白单元格的情况下计算经过过滤的列中唯一/不同的可见值? - How to count unique/ distinct visible values in a filtered column, with considering blank cells?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM