VBA代码可根据用户输入框对列字母数字和日期应用过滤器

时间:2018-07-02 14:25:55

标签: vba date input autofilter

我想编写vba代码,询问用户他们要过滤的“哪一列”;然后他们要过滤“什么日期”(即mm / dd / yyyy之后的任何日期)

类似的东西:

Sub FilterByInput()

Dim iPutFound As Range
Dim mycol As String
Dim mydate As String

mycol = Application.InputBox("Enter alpha-numeric of Column to be filtered -ie. AP3", Type:=2)
mycol = Range(mycol & 1).Column
mydate = Application.InputBox("Enter date since last update was ran- mm/dd/yyyy", Type:=2)
Application.ScreenUpdating = False
ActiveSheet.Range("$A$3:$DK$11000").AutoFilter Field:=mycol, Criteria1:=">" & mydate, Operator:=xlFilterValues

'The code will resume with copying and pasting the filtered results to another sheet, etc

End Sub

我收到运行时错误:“范围类的自动筛选器方法失败。”我的标题行在第3行。我尝试在运行代码之前关闭过滤器,但这也不起作用。任何帮助将不胜感激。

这是我所做的修改:

Dim ws As Worksheet: Set ws = ActiveSheet   
Dim iPutFound As Range  
Dim mycol As String  
Dim myrng As Range  
Dim mydate As String  

mycol = Application.InputBox("Enter alpha-numeric of Column to be filtered -ie. M", Type:=2)  
Set myrng = Range(mycol & 1).Column
mydate = Application.InputBox("Enter date since last update was ran- mm/dd/yyyy", Type:=2)
Application.ScreenUpdating = False
ActiveSheet.Range(mycol & 1).Column.AutoFilter Field:=mycol, Criteria1:=">" & mydate, Operator:=xlFilterValues

0 个答案:

没有答案