【VBA研究】工作表自己主动筛选模式检測

作者:iamlaosong

用VBA程序处理Excel数据文件。用户的数据文件有时处于自己主动筛选模式,往往导致数据处理不对。为此,须要检測工作表是否处于该模式,假设是,则去掉自己主动筛选。语句例如以下:

If ActiveSheet.AutoFilterMode = True Then Selection.AutoFilter

这个语句一般仅仅用于去掉自己主动筛选,尽管Selection.AutoFilter也能够加上自己主动筛选,但筛选位置却可能在当前单元格处,所以要注意。加自己主动筛选前,现将单元格定位到字段标题处。然后用例如以下语句:

If ActiveSheet.AutoFilterMode = False Then Selection.AutoFilter

假设检測其他非活跃的工作表,能够用以下语句:

If Worksheets("sheet1").AutoFilterMode = True Then

Worksheets("sheet1").Range("A1").AutoFilter

附:

Range.AutoFilter Method

Excel Developer Reference
 

Filters a list using the AutoFilter.

Syntax

expression.AutoFilter(Field, Criteria1, Operator, Criteria2, VisibleDropDown)

expression   An expression that returns a Range object.

Parameters

Name Required/Optional Data Type Description
Field Optional Variant The integer offset of the field on which you want to base the filter (from the left of the list; the leftmost field is field one).
Criteria1 Optional Variant The criteria (a string; for example, "101"). Use "=" to find blank fields, or use "<>" to find nonblank fields. If this argument is omitted, the criteria is All. If Operator is xlTop10Items, Criteria1 specifies
the number of items (for example, "10").
Operator Optional XlAutoFilterOperator One of the constants of XlAutoFilterOperator specifying the type of filter.
Criteria2 Optional Variant The second criteria (a string). Used with Criteria1 and Operator to
construct compound criteria.
VisibleDropDown Optional Variant True to display the AutoFilter drop-down arrow for the filtered field. False to
hide the AutoFilter drop-down arrow for the filtered field. True by default.

Return Value

Variant

Remarks

If you omit all the arguments, this method simply toggles the display of the AutoFilter drop-down arrows in the specified range.

Example

This example filters a list starting in cell A1 on Sheet1 to display only the entries in which field one is equal to the string "Otis". The drop-down arrow for field one will be hidden.

Visual Basic for Applications
Worksheets("Sheet1").Range("A1").AutoFilter _
    field:=1, _
    Criteria1:="Otis", _
    VisibleDropDown:=False

时间: 2025-01-02 17:41:34

【VBA研究】工作表自己主动筛选模式检測的相关文章

Excel vba引用工作表的三种写法

文章介绍vba引用工作表名称的三种不同写法. vba引用工作表是我们在学习VBA过程中很常用. 本文提供三种vba引用工作表的代码,通过这三种方式都可以实现vba引用工作表名. 方法一:Sheets(Sheet.Index) 方法二:Sheets(Sheets.Name) 方法三:Sheets.CodeName 下面是vba引用工作表的相关的一个截图,可以很直观的看到其使用. 原文地址:https://www.cnblogs.com/huhewei/p/9039168.html

exel VBA拆分工作表

客户经理每个月要拜访很多客户,公司要求必须要一个拜访记录汇总表并且要做一个拜访客户的分表,以便主管抽查,表的结构如图一.这个时候如果一个客户一个客户填的话就很烦很耗时间.我们可以做一个VBA按钮,每个月只要把汇总部分填好后,只要点击一下该按钮,就自动生成和客户信息的分表,省力又省心. 图一 先贴代码: Sub cfsheet() Dim rng As Range, sht As Worksheet Set rng = Application.InputBox("请选择需要拆分的列",

Excel VBA 判断工作表是否为空或被使用过(比如设置过框线)

IsEmpty 函数 返回 Boolean 值,指出变量是否已经初始化. [语法] IsEmpty(expression) 必要的 expression 参数是一个 Variant,包含一个数值或字符串表达式(实际上还可以是单元格).但是,因为 IsEmpty 被用来确定个别变量是否已初始化,所以 expression 参数通常是单一变量名. [说明] 如果变量未初始化或已明确设置为 Empty,则 IsEmpty 返回 True:否则返回 False.如果 expression 含有多个变量,

VBA取消工作表或工作薄密码保护

Sub 取消工作表保护() Dim sht As Worksheet For Each sht In Worksheets sht.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True , AllowFiltering:=True, AllowUsingPivotTables:=True sht.Protect DrawingObjects:=False, Contents:=True, Scenarios:= False,

【VBA研究】工作表自动筛选模式检测

作者:iamlaosong 用VBA程序处理Excel数据文件,用户的数据文件有时处于自动筛选模式,往往导致数据处理不正确,为此,需要检测工作表是否处于该模式,如果是,则去掉自动筛选,语句如下: If ActiveSheet.AutoFilterMode = True Then Selection.AutoFilter 这个语句一般只用于去掉自动筛选,虽然Selection.AutoFilter也可以加上自动筛选,但筛选位置却可能在当前单元格处,所以要注意,加自动筛选前,现将单元格定位到字段标题

【VBA研究】统计Excel 工作表数量

作者:iamlaosong VBA编程中如果对每一个工作表循环处理,就需要知道工作表的数量,常见的方法如下: 1.当前工作簿中工作表数量 num = ThisWorkbook.Sheets.Count 这当前工作簿就是VBA程序所在的工作簿,那么其它工作簿中工作表的数量如何统计呢? 2.活动工作簿中工作表数量 Windows(datfile).Activate num = ActiveWorkbook.Sheets.Count 3.知道文件名的工作簿中工作表数量 datfile = Cells(

月汇总工作表 VBA

+ ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

Excel VBA在生成副本的工作表中插入本工作簿中的VBA模块代码

即在工作簿中添加一个工作表,然后移出并存为新的工作簿,在移出前将本工作簿的一个模块的代码拷贝至新的工作簿.下面是关键代码: '====================================================================== '各班名单保存为单个xls文件 ActiveSheet.Move ChDir myPath '忽略对话框,覆盖保存 Application.DisplayAlerts = False '班级名称增加"考生号处理"vba模块

【VBA】获取当前工作表的用户名

如何使用VBA获取当前工作表的用户名呢?请看如下代码: Sub 获取当前工作表的用户名() MsgBox "当前工作表的用户名为:" & Application.UserName End Sub 点击运行程序后,效果如下: 点击下载附件