简体   繁体   English

动态Excel VBA过滤器

[英]Dynamic Excel VBA filter

I have one macro which filters raw data according to fix criteria. 我有一个宏,可以根据修复标准过滤原始数据。 I have assigned this macro on one button (Move In). 我已将这个宏分配给一个按钮(移入)。

在此处输入图片说明

this macro is showing the details raw data for cell D11 (41 value) code is like 此宏显示的是单元格D11(41值)代码的详细信息原始数据,例如

Sub Jan_in()
Application.ScreenUpdating = False
Dim sStradd As String
Sheets("Hiring_Attrition").Activate
Range("A3").Select
sStradd = ActiveCell.CurrentRegion.Address
ActiveSheet.Range(sStradd).AutoFilter Field:=1, Criteria1:="Jan-17"
Call clear
Sheets("Hiring_Attrition").Activate
ActiveSheet.Range("B3:H3").Select
Sheets("Hiring_Attrition").Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Sheets("Report").Range("B8")
Selection.AutoFilter
Sheets("Report").Activate
Sheets("Report").Range("B:H").EntireColumn.AutoFit
Sheets("Report").Range("A1").Select
Application.ScreenUpdating = True
End Sub `

I have multiple macros like this but now I want a macro which pick filter according to cell reference. 我有多个这样的宏,但是现在我想要一个根据单元格引用选择过滤器的宏。 Like Macro will take criteria one as -2 row of same column for which selected cell is and criteria two -2 column of same row for which selected cell. 像宏一样,将条件1作为选定单元格所在的同一列的-2行,将条件2作为选定单元格同一行的-2列。

Let me know if any further clarification is need. 让我知道是否需要进一步澄清。

Dynamic Filter: 动态过滤器:

I have a sheet with a drop down selector (which I built by this instruction: http://www.techrepublic.com/blog/microsoft-office/how-to-add-a-drop-down-list-to-an-excel-cell/ ). 我有一个带有下拉选择器的工作表(我是根据以下说明构建的: http : //www.techrepublic.com/blog/microsoft-office/how-to-add-a-drop-down-list-to-an -excel-cell / )。 Filter can be modified based on which item is selected. 可以根据选择的项目来修改过滤器。 Code looks like this: 代码如下:

For i = 1 To 10
If Range("SELECTOR").Value = Sheets("sheet2").Range("a" & i).Value Then
    Selected = i
End If
Next

The above code identifies which value from the list is selected. 上面的代码标识从列表中选择哪个值。 "SELECTOR" is the drop down cell, and we're stepping through 10 cells on the second sheet to determine which row number was selected. “ SELECTOR”是下拉单元格,我们将逐步浏览第二张工作表上的10个单元格,以确定选择了哪个行号。 Now, you can use that info to customize your filter. 现在,您可以使用该信息来自定义过滤器。 For example: 例如:

If Selected = 7 Then
 Range("A4:Z1000").AutoFilter
 Range("A4:Z1000").AutoFilter field:=1, Criteria1:="", visibledropdown:=True
Else
 Range("A4:Z1000").AutoFilter
 Range("A4:Z1000").AutoFilter field:=Selected + 1, Criteria1:="x", 
 visibledropdown:=True
End If

The reason I do the empty AutoFilter first, is because it clears the previous filter. 我之所以先做空的AutoFilter,是因为它清除了以前的过滤器。 Criteria = "x" because that's what I was filtering by, but you could sub in a variable to make it dynamic. Criteria =“ x”,因为这就是我过滤的依据,但是您可以在变量中添加一个子变量以使其动态。

Hope this helps. 希望这可以帮助。

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

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