2011-02-24 12:22:53 +0000 2011-02-24 12:22:53 +0000
15
15

当数据发生变化时,如何自动刷新Excel自动过滤?

当数据发生变化时,如何自动刷新Excel自动过滤?

用例。我将一个单元格的值改为一个被过滤的值。我想看到当前行消失,而不需要做任何其他事情。

答案 (7)

7
7
7
2012-08-09 15:31:24 +0000

换成这样的代码似乎也能解决这个问题(至少在Excel 2010中):

Private Sub Worksheet_Change(ByVal Target As Range)

    ActiveSheet.AutoFilter.ApplyFilter

End Sub
```。
4
4
4
2012-11-06 18:12:51 +0000

我发现,当我使用表格时,这并不奏效。过滤器不在工作表上,而是在表上。这段代码做到了

Private Sub Worksheet_Change(ByVal Target As Range)
    With ActiveWorkbook.Worksheets("Sheet1").ListObjects("Table1")
         .AutoFilter.ApplyFilter
    End With
End Sub

我在这里找到了相关信息。 http://www.jkp-ads.com/articles/Excel2007TablesVBA.asp

1
1
1
2017-06-10 10:08:26 +0000

我也用了一个基于Worksheet_Change事件的VBA/Macro,但我的方法略有不同…… 好吧,先说代码,再说解释。

Private Sub Worksheet_Change(ByVal Target As Range)
    ' first remove filter
    ActiveSheet.Range("$L$1:$L$126").AutoFilter Field:=1        
    ' then apply it again
    ActiveSheet.Range("$L$1:$L$126").AutoFilter Field:=1, Criteria1:="<>0"
End Sub

(使用Alt+F11组合键使开发面板出现,并将代码粘贴到包含你想要自动刷新的过滤器的工作表中。)

在我的例子中,我假设在单列(在我的例子中是L)上有一个简单的过滤器,我的数据范围是在1(即使它可能包含标题)到126(选择一个足够大的数字来确定)的行上。操作很简单:当我的工作表发生变化时,指定范围内的过滤器会被删除/重新应用,以便刷新。这里需要解释一下的是FieldCriteria

Field是一个范围的整数偏移。在我的例子中,我只有一个单列过滤器,而范围是由单列(L)构成的,它是范围中的第一个(因此我用1作为值)。

Criteria是一个字符串,描述了要应用到数据范围的过滤器。在我的例子中,我想只显示L列与0不同的行(因此我使用"<>0")。

就这样。关于Range.AutoFilter方法的更多参考,请看。 https://msdn.microsoft.com/en-us/library/office/ff193884.aspx

1
1
1
2011-02-27 15:19:59 +0000

右键点击你的工作表名称,选择 “查看代码",然后粘贴下面的代码。粘贴完毕后,点击左上方 "文件 "下方的Excel图标,或输入Alt-F11,返回电子表格视图。

这将启用自动刷新功能。不要忘了以支持宏的格式保存文件,.xlsm

Private Sub Worksheet_Change(ByVal Target As Range)

    If Me.FilterMode = True Then
        With Application
           .EnableEvents = False
           .ScreenUpdating = False
        End With

        With ActiveWorkbook
            .CustomViews.Add ViewName:="Mine", RowColSettings:=True
          Me.AutoFilterMode = False
            .CustomViews("Mine").Show
            .CustomViews("Mine").Delete
        End With

         With Application
           .EnableEvents = True
           .ScreenUpdating = True
        End With
    End If

End Sub
0
0
0
2016-12-16 14:59:00 +0000

巩固一下答案:

Sorin说:

右击你的工作表名称,选择 “查看代码",然后粘贴下面的代码,粘贴后,点击左上方 "文件 "下面的Excel图标,或者输入Alt-F11,返回电子表格视图。

这将启用自动刷新。不要忘记将文件保存为支持宏的格式,即.xlsm。

Chris用了这个代码(我在2010年刚做的):

Private Sub Worksheet_Change(ByVal Target As Range)

    ActiveSheet.AutoFilter.ApplyFilter

End Sub

如果你不展开帖子,你只能看到长长的答案! ;)

-1
-1
-1
2019-05-16 13:00:33 +0000
using "data, from table"/power query in excel, which gives us option to refresh data when opening file. (also auto sort, and index column (number filtered rows automatically )) This will create result in another sheet. -select data required using mouse (rows and columns) -click on data tab, from table -in the last column, exclude blanks (optional, if you want to to display only filled cells) -add column, index column (optional, if you want to add row number to filtered results) -close and load to to edit again, click on query tab, and then on edit click on design tab in excel, on the arrow below refresh, connection properties, refresh data when opening file. adapted from: https://www.excelcampus.com/tips/sort-drop-down-lists-automatically/ part: 3. Sorting Drop Down Lists Using Power Query you can also copy data from sheet1 if not empty, for example field a1. copy this to a1 field in sheet2: =IF(Sheet1!A1"";Sheet1!A1;"")
-1
-1
-1
2017-08-27 20:47:46 +0000

抱歉,回复不足,无法评论。(管理员,请随意将其剪切成上面的评论。)用户 “danicotra "的回答以 "我也使用基于工作表/修改事件的VBA/宏观,但我的方法…… "开头的 ‘ 先删除过滤器 ’ 然后再应用 是使用Excel 2007+时的正确解决方案。但是.AutoFilter.ApplyFilter在XL03和更早的版本中是无效的,所以我展示了下面的方法。

我恳请真正的专家和大师阅读这段代码,因为我很自信这是顶级的素材。也许当大家看到下面的好东西时,这个答案的莫名其妙的降权数可以逆转。

danicotra用了一个简化的例子。其实,你可以做得更一般。假设用ActiveSheet进行以下操作(或其他一些表对象):

1.保存自动过滤器的范围。它有.AutoFilter.Filters.Count列,和(.AutoFilter.Range.Count/.AutoFilter.Filters.Count)行,保存到rngAutofilter

  1. 在数组myAutofilters中收集.AutoFilter.Filters.Count自动过滤项目的4个属性中的每一个,注意避免在.On或.Operator为false时出现 "应用程序定义的错误"。(myAutofilters将被重新Dim'd为步骤1中的行数和列数)

  2. 关闭过滤器,但用.ShowAllData保留下拉菜单

  3. 对于根据你保存的数组的每个过滤项目是.On,重置4个属性中的3个.AutoFilter.Filters.Count自动过滤项目。再次注意,当.Operator为false时,要避免 "应用程序定义的错误",所以对于每个项目 "i", rngAutofilter.AutoFilter Field:=i,Criteria1:=myAutofilters(i,2) 或 rngAutofilter.AutoFilter.Count自动过滤项目。 自动过滤字段:=i, Criteria1:=myAutofilters(i,2), Operator:=myAutofilters(i,3), Criteria2:=myAutofilters(i,4)

现在,自动过滤将被重新启动,范围与代码开始前相同,但自动过滤会根据数据变化进行更新。

Public myAutofilters As Variant, rngAutofilter As Range 'Public
Sub SaveAndRestoreAutofilters()
  'This will update the autofilter display to recognize data changes by turning autofilter off and then on, preserving all characteristics
  'Note, XL2007 and later have .autofilter.applyfilter, but not the invaluable XL03 and earlier
  Dim i As Long, iNumAutofilters As Long, iNumActiveAutofilters As Long
  iNumActiveAutofilters = SaveAutoFilterInfo(iNumAutofilters) 'NOTE! Use CALL or assignment to prevent parentheses from forcing ByVal !
  If iNumActiveAutofilters < 1 Then
      Application.StatusBar = "0 ACTIVE filters;" & iNumAutofilters & " autofilters"
      Exit Sub
  End If
  ActiveSheet.ShowAllData

  Rem Here optionally do stuff which can include changing data or toggling autofilter columns

  For i = 1 To iNumAutofilters
      If myAutofilters(i, 1) Then
          If myAutofilters(i, 3) <> 0 Then 'then .Operator is something, so set it and Criteria2, else just Criteria1
              rngAutofilter.AutoFilter Field:=i, Criteria1:=myAutofilters(i, 2), Operator:=myAutofilters(i, 3), Criteria2:=myAutofilters(i, 4) ', On:=true by rule
          Else
              rngAutofilter.AutoFilter Field:=i, Criteria1:=myAutofilters(i, 2) ', On:=true by rule (it's R/O anyway)
          End If
          Rem Selection.AutoFilter Field:=i 'How you'd "turn off" only a single column's autofiltering. FYI .On is R/O!
      End If
      'activesheet.autofiltermode=false 'just FYI, how you comprehensively turn off filtering on a sheet (erasing the dropdowns and criteria and filter range!)
  Next i
End Sub
Function SaveAutoFilterInfo(iNumAutofilters As Long) As Long
  Dim i As Long, iRowsAutofiltered As Long
  SaveAutoFilterInfo = 0 'counts the number that are .On, and returns the total
  iNumAutofilters = ActiveSheet.AutoFilter.Range.Columns.Count
  If ActiveSheet.AutoFilter.Filters.Count <> iNumAutofilters Then MsgBox "I can't explain this. All bets are off. Aborting.": Exit function
  ReDim myAutofilters(1 To iNumAutofilters, 4)
  For i = 1 To iNumAutofilters
      myAutofilters(i, 1) = ActiveSheet.AutoFilter.Filters(i).On
      If myAutofilters(i, 1) Then
          SaveAutoFilterInfo = SaveAutoFilterInfo + 1
          myAutofilters(i, 2) = ActiveSheet.AutoFilter.Filters(i).Criteria1
          myAutofilters(i, 3) = ActiveSheet.AutoFilter.Filters(i).Operator
          If myAutofilters(i, 3) <> 0 Then 'then is either xlAnd, xlOr, etc., and there's a second criteria
              myAutofilters(i, 4) = ActiveSheet.AutoFilter.Filters(i).Criteria2
          End If
      End If
  Next i
  iRowsAutofiltered = ActiveSheet.AutoFilter.Range.Count / ActiveSheet.AutoFilter.Range.Columns.Count
  Set rngAutofilter = Cells(ActiveSheet.AutoFilter.Range.Row, ActiveSheet.AutoFilter.Range.Column).Resize(iRowsAutofiltered, iNumAutofilters)
End Function