简体   繁体   English

使用VBA自动过滤另一张纸

[英]Autofilter in another sheet using VBA

I intend to filter values that begins with 314 in column F, and clear its contents(entire row). 我打算在F列中过滤以314开头的值,并清除其内容(整行)。 The workbook has 30,000+ rows and I think looping is not a good option when filtering in another sheet(sA). 该工作簿有30,000多行,我认为在另一个工作表中进行过滤时,循环不是一个好的选择。 I recorded the following code below. 我在下面记录了以下代码。

Sheets("sA").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$AF$30436").AutoFilter Field:=6, Criteria1:="=314*" _
    , Operator:=xlAnd
ActiveCell.Offset(-181, -2).Range("A1:AF30436").Select
ActiveCell.Activate
Selection.ClearContents

When I ran the code, a Runtime Error 1004 appears. 当我运行代码时,出现运行时错误1004。 I think because of the ActiveCell, because I ran the code in a different sheet(sB, where the button for filtering sheets in sA is found). 我认为是因为有ActiveCell,因为我在不同的工作表(sB,在sA中找到用于过滤工作表的按钮)中运行了代码。 What could be the possible fix to this? 有什么可能的解决办法? Any suggestions? 有什么建议么?

this should work , 这应该工作,

Sub filter()
   Dim ws As Worksheet
   Set ws = Sheets("sheet1")

   ws.Range("$A$1:$AF$30436").AutoFilter Field:=6, Criteria1:="=314*" _
    , Operator:=xlAnd

   Dim LR As Long
   LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
   ws.Range("A2:AF" & LR).SpecialCells(xlCellTypeVisible).ClearContents
   ws.AutoFilterMode = False
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM