我正在过滤辅助单元格,以查找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
答案 0 :(得分:1)
这就是我要做的:
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