Excel 清除过滤列中可见单元格的内容

Excel 清除过滤列中可见单元格的内容,excel,vba,Excel,Vba,我正在筛选一个辅助单元格,以查找B列中需要清除内容的单元格。一旦我在助手单元格上进行筛选,该单元格已识别出B列中需要清除内容的单元格,则我在清除该单元格中的内容时遇到问题 我记下了大概的想法,只是我不知道如何从第一个可见单元格到最后一个可见单元格清除可见单元格。我的问题是确定应用过滤器后第一个可见单元格的起始位置和最后一个可见单元格的位置 Sub Macro1() ' ' Macro1 Macro Dim wb As Workbook Dim ws As Worksheet Dim Found

我正在筛选一个辅助单元格,以查找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
我是这样做的:

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
Sub-tgr()
将wb设置为工作簿
将ws设置为工作表
Dim FoundCell1 As范围
设置wb=ActiveWorkbook
设置ws=wb.ActiveSheet
'这将标识要筛选的最后一个单元格的行
Const WHAT_TO_FIND1为String=“Tango”
Set FoundCell1=ws.Range(“AX:AX”).Find(What:=What-TO-FIND1)
如果FoundCell1为Nothing,则退出Sub“WHAT_TO_FIND1 not found”
'这是对辅助单元格进行筛选,以确定需要清除哪些单元格。
使用ws.Range($BA$8:$BA$”&FoundCell1.Row)
如果.Row<8或.Rows.Count=1,则退出Sub“无数据”
.自动筛选字段:=1,标准1:=“删除”
错误时继续下一步“在没有可见单元格的情况下抑制错误”
相交(.Worksheet.Columns(“B”),.Offset(1)。调整大小(.Rows.Count-1)。EntireRow)。特殊单元格(xlCellTypeVisible)。ClearContents
错误时转到0'删除“错误时继续下一步”条件
.自动过滤器
以
端接头

感谢您的代码虎。我尝试了你的代码,似乎自动筛选是在A列而不是BA列上进行筛选…这没有意义,因为代码似乎引用了BA。我之前也遇到过这个问题,我引用了要过滤的BA,但它一直过滤A。起初我以为这只是我的代码,但这是Excel中的一个小故障吗?@JDoe我无法复制这种行为。这对我来说是正确的。啊,我明白了,这是我的问题。导致此问题的辅助行中有一个空行将标题与其他单元格分隔开。我只需要在空行中插入内容,将标题连接到helper列中的其余数据,您的宏就可以完美地运行了。非常感谢你!