简体   繁体   English

Excel Macro Autofilter问题与变量

[英]Excel Macro Autofilter issue with variable

I have a table of data with the top row being filters, I have a loop that changes which filter needs to be used inside the loop is the variable filterColumn that is being assigned a new value every time the loop runs through. 我有一个数据表,其中第一行是过滤器,我有一个循环,该循环更改需要在循环内部使用的过滤器是变量filterColumn,每次循环运行时都会为其分配一个新值。

when i try to use filterColumn to determine which filter will be 'switched on' i get an error 当我尝试使用filterColumn确定将“打开”哪个过滤器时,出现错误

Autofilter method of Range Class Failed 范围类别的自动筛选方法失败

ActiveSheet.Range("$U$83:$CV$1217").AutoFilter Field:=filterColumn, Criteria1:="<>"

What is the correct syntax in order to use a variable to determine which field the filter is in? 为了使用变量确定过滤器位于哪个字段中,正确的语法是什么?

Problem Solved I found the solution. 解决问题我找到了解决方案。 I was referencing the filters columns position in terms of the whole worksheet when in fact I should have been referencing what number it was in the group of filters. 我实际上是在整个过滤器中引用过滤器列的位置,而实际上我应该在过滤器组中引用它的编号。 For example the filter I wanted to change was in 'CF' which is the 84th column but my the filter I wanted to change is the 64th in the group. 例如,我要更改的过滤器位于“ CF”列中的第84列中,但我要更改的过滤器在该组中排名第64位。

Dim filterColumn As Integer
filterColumn = 2
ActiveSheet.Range("$U$83:$CV$1217").AutoFilter Field:=filterColumn, _
    Criteria1:="<>"

EDIT : I tried @HeadofCatering's solution and initially it failed. 编辑 :我尝试@HeadofCatering的解决方案,并最初失败了。 However I filled in values in the referenced columns and it worked (my solution also failed under reverse conditions - make the column headers blank and it fails). 但是,我在引用的列中填充了值,并且它起作用了(我的解决方案在相反的条件下也失败了-将列标题设置为空白,它将失败)。

However this doesn't quite mesh with what I've (and probably you've) seen - you can definitely add filters to columns with blank headers. 但是,这与我(也许您已经)看到的内容不太吻合-您绝对可以为带有空白标题的列添加过滤器。 However one thing was consistent in the failures I saw - the filterColumn referenced a column that was outside of Application.UsedRange . 但是,有一件事情与我看到的失败是一致的filterColumn引用了Application.UsedRange之外的列。 You may want to try verifying that the column you are referencing is actually within Application.UsedRange (easy way: run Application.UsedRange.Select in the Immediate Window and see if your column is selected). 您可能想要尝试验证您所引用的列是否确实在Application.UsedRange (简便方法:在“即时窗口”中运行Application.UsedRange.Select ,然后查看是否已选择该列)。 Since you are referencing a decent amount of columns, it is possible that there are no values past a certain point (including column headers), and when you specify the column to filter, you are actually specifying something outside of your UsedRange . 由于您要引用大量的列,因此可能没有某个点(包括列标题)之后的值,并且当您指定要过滤的列时,实际上是在UsedRange之外指定了某些UsedRange

An interesting (this is new to me as well) thing to test is taking a blank sheet, filling in values in cells A1 and B1 , selecting columns A:G and manually adding AutoFilters - this will only add filters to columns A and B (a related situation can be found if you try to add filters to a completely blank sheet). 要测试的一件有趣的事情(这对我也是新的),是一张空白纸,在单元格A1B1填写值,选择列A:G并手动添加自动过滤器-这只会将过滤器添加到列A和B(如果您尝试将过滤器添加到完全空白的工作表中,则会发现相关情况)。

Sorry for the babble - chances are this isn't even your problem :) 对不起您的胡言乱语-这可能甚至不是您的问题:)


Old solution (doesn't work when conditions described above are used) 旧的解决方案(使用上述条件时不起作用)

I may be overkilling it, but try setting the sheet values as well (note I used a sample range here): 我可能对此过于矫kill过正,但也请尝试设置工作表值(请注意,我在此处使用了示例范围):

Sub SOTest()

Dim ws As Worksheet
Dim filterColumn As Integer

' Set the sheet object and declare your variable
Set ws = ActiveSheet
filterColumn = 2

' Now try the filter
ws.Range("$A$1:$E$10").AutoFilter Field:=filterColumn, Criteria1:="<>"

End Sub

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

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