首页 > 解决方案 > VBA 代码基于用户输入框应用过滤器,用于列字母数字和日期

问题描述

我想编写询问用户他们想要过滤“哪一列”的 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

标签: vbadateinputautofilter

解决方案


推荐阅读