简体   繁体   English

如果在自动过滤器中找不到条件,如何停止

[英]How to stop if Criteria isn't found in autofilter

My code gather info based on a different file that's just data and copies and pastes the data in my main folder. 我的代码基于仅是数据的另一个文件收集信息,然后将数据复制并粘贴到我的主文件夹中。 It goes of the Dim which I have set to a range B1 but right now if the data in B1 isn't found in the Field I'm looking for it simply copies all the blank cells into the main folder. 我已将Dim设置为范围B1,但现在,如果我要查找的字段中未找到B1中的数据,只需将所有空白单元格复制到主文件夹中即可。

I would need it to stop if the Variable isn't located in the field 如果变量不在字段中,我将需要它停止

Sub Sheet01()
'
' Sheet01 Macro
'

'
Application.ScreenUpdating = False
Dim varField            As String
varField = Range("B1")
With ActiveSheet
  Range("Q3:AA2999").Select
    Selection.ClearContents
    Workbooks.Open Filename:= _
        "V:\Training\Share\Intraday Training Tracker\Agent Suite Raw        Data\AgentSuite - Current.xlsx"
Sheets("ActivityDetails").Select
ActiveSheet.Range("$A$1:$K$197561").AutoFilter Field:=4, Criteria1:=Array( _
    "CS", "Cons", "Medium", "MobOutbound", "NC", _
    "PMobile", "Personal Team", "ST", "SmBu", "Tech", _
    "Tech Support "), Operator:=xlFilterValues
ActiveSheet.Range("$A$1:$K$197561").AutoFilter Field:=10, Criteria1:=Array( _
    "Complete", "Eligible", "NotScheduled", "Processing", "Scheduled"), Operator:=xlFilterValues
ActiveSheet.Range("$A$1:$K$197561").AutoFilter Field:=11, Criteria1:="1"
ActiveSheet.Range("$A$1:$K$197561").AutoFilter Field:=1, Criteria1:=varField

Currently I don't get any error message . 目前我没有收到任何错误消息。

You will not get an error if the filter does not find any values. 如果过滤器找不到任何值,则不会出现错误。 I would suggest that after you open the workbook and select the sheet (I suggest looking up how to avoid Activate/Select) that you do a Range.Find to determine if the varField exists. 我建议您在打开工作簿并选择工作表之后(我建议您查找如何避免激活/选择),然后执行Range.Find以确定varField存在。 If it does not, then you can exit the Sub or do something else. 如果不是,则可以退出Sub或执行其他操作。

Set FindRng = Sheets("ActivityDetails").Columns(1).Find(varField)
If FindRng Is Nothing Then
    Exit Sub
End If

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

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