我有大量数据,我必须在任何时间点过滤特定月份的数据。我们的想法是弹出一个问题,询问应该在哪个月生成报告,然后使用该值来过滤所需的数据。更好的是,如果我能得到excel只给我那个月的数据并删除其余的。 (大约80K行,从2014年到现在的日期)。
Sub FilterByReportingMonth()
Dim wsreport As Worksheet
Dim ReportingMonth As String
Dim UserInputMonth As Long
Set wsreport = Sheets("Report")
Range("U:U").NumberFormat = "mm/yyyy"
ReportingMonth = InputBox("Which month's Classroom Report do you want to generate? Please provide in mm/yyyy format.")
If ReportingMonth = vbNullString Then Exit Sub
If Not IsDate(ReportingMonth) Then
MsgBox "That is not a valid date. Please use mm/yyyy format"
Run "FilterByDateRange"
End If
UserInputMonth = CDate(ReportingMonth)
'the below is in red and i get a syntax error
wsreport.Range("$A$9:$AF$99999").AutoFilter Field:=21, Criteria1:= _
"UserInputMonth", Operator:=xlAnd,
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
End Sub
答案 0 :(得分:0)
从以下内容开始:
Option Explicit
Sub FilterByReportingMonth()
Dim wsreport As Worksheet
Dim ReportingMonth As String
Dim UserInputMonth As Date
Set wsreport = ThisWorkbook.Sheets("Report") 'assuming code in same workbook as data
With wsreport
.Range("U:U").NumberFormat = "mm/yyyy"
ReportingMonth = InputBox("Which month's Classroom Report do you want to generate? Please provide in mm/yyyy format.")
If ReportingMonth = vbNullString Then Exit Sub
If Not IsDate(ReportingMonth) Then
MsgBox "That is not a valid date. Please use mm/yyyy format"
Run "FilterByDateRange"
End If
UserInputMonth = CDate(ReportingMonth)
.Range("$A$9:$AF$99999").AutoFilter
.Range("$A$9:$AF$99999").AutoFilter Field:=21, Operator:= _
xlFilterValues,
Criteria1:=Format$(UserInputMonth, "mm/yyyy")
End With
End Sub