Excel 如何在通过VBA进行自动筛选时跳过字段?
我有一个过滤过程,如果某些列不包含相关数据,我想用它跳过过滤。这是我的代码,后面是我的描述和问题:Excel 如何在通过VBA进行自动筛选时跳过字段?,excel,vba,criteria,autofilter,Excel,Vba,Criteria,Autofilter,我有一个过滤过程,如果某些列不包含相关数据,我想用它跳过过滤。这是我的代码,后面是我的描述和问题: Sub FilterData() 'First I select my values in a pivot table in my Dashboard sheet. ' Cell references P7, P8 and P9 are fields in this pivot; Myvalue1 = Sheets("Dashboard").Range("P7")
Sub FilterData()
'First I select my values in a pivot table in my Dashboard sheet.
' Cell references P7, P8 and P9 are fields in this pivot;
Myvalue1 = Sheets("Dashboard").Range("P7")
Myvalue2 = Sheets("Dashboard").Range("P8")
Myvalue3 = Sheets("Dashboard").Range("P9")
Application.ScreenUpdating = False
'Next I go to my dataset that the pivot is based upon in another sheet
' to filter the data according to my selected values from before.
Sheets("Data").Visible = True
Sheets("Data").Select
'First I clear existing filters if applicable, then I start to sort the data.
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.ShowAllData
ActiveSheet.Range("$A$1:$BR$30000").AutoFilter Field:=12, Criteria1:=Myvalue1
ActiveSheet.Range("$A$1:$BR$30000").AutoFilter Field:=66, Criteria1:=Myvalue2
ActiveSheet.Range("$A$1:$BR$30000").AutoFilter Field:=67, Criteria1:=Myvalue3
Application.ScreenUpdating = True
End Sub
备注:现在,我的名为
Data
的工作表将根据我在pivot中的选择显示我想要的摘录。但是,如果我选择了而不是,例如在P7
中进行选择(因此数据透视表在单元格P7中显示(全部)),则所有内容都会被过滤掉,并且没有数据显示
我之所以想过滤原始数据,而不是双击透视以查看详细数据,是因为我希望能够编辑我选择的数据,并在以后保存更改
问题:如果
单元格P7
的值为(全部)
,我如何告诉我的代码让列12
(Myvalue1
)保持未过滤状态?
谢谢 欢迎光临!如果您可以在屏幕上显示您的数据示例以及问题,这将非常有帮助。下面是有关如何创建问题的提示。此外,请务必查看(您将获得您的第一个徽章!),以及。只需猜测而不查看您的数据,但是一个简单的
If
语句(与您正在使用的语句非常类似)怎么样,例如If P7“(All)”Then ActiveSheet.Range($a$1:$BR$30000”)。自动筛选字段:=12,准则1:=Myvalue1
您已经回答了自己的问题:)如果范围(“P7”)=“(全部)”则不结束If。或者更现实地说,如果范围(“P7”)“(All)”那么dodostuff结束如果问题解决了!感谢阿什利多格和苦艾酒的投入。这就是我最后得到的结果:如果Myvalue1“(全部)”,那么ActiveSheet.Range($A$1:$BR$30000”)。自动筛选字段:=12,标准1:=\Myvalue1 End If