excel - 在列中的日期之间自动过滤 + 另一个未填充目标的条件
问题描述
我有一个数据表(名为 BI_Comissoes 的表),其中有日期列和推销员列。然后,我得到了目标表,该表用作根据在 B7 和 B8 上输入的日期范围以及在 B5 上输入的推销员姓名填充的报告。运行代码后,不会填充目标表,但会正常过滤数据表。该代码以前可以工作,但我想我在某个地方弄乱了它,我的神经元似乎被烧毁了,因为我无法找出问题所在。
我尝试了许多日期格式,但似乎没有任何效果。供您参考,数据表上的日期为 dd/mm/yyyy。
Sub SelectDataBetweenTwoDates()
'declare variables
Dim fromDate, toDate As Date
Dim MyResults As Worksheet, MyData As Worksheet, MyDates As Worksheet
Set MyResults = Worksheets("Relatório de Comissão")
Set MyData = Worksheets("BI_Comissoes")
Set MyDates = Worksheets("Relatório de Comissão")
'clear previous results
'MyResults.Cells.Clear
'attribute date values to variables
fromDate = MyDates.Range("B7").Value
toDate = MyDates.Range("B8").Value
Vendedor = MyResults.Range("B5").Value
'convert to text format to allow filtering
fromDate = Format(fromDate, "dd-mmm-yyyy")
toDate = Format(toDate, "dd-mmm-yyyy")
'clear previous filtered data
With MyResults
'If fromDate = "" Or toDate = "" Then
'MsgBox "Informar o período!"
'Exit Sub
'Else: Resume
On Error Resume Next
Err.Number = 0
.Range("A$17:$K$20000").Select
Intersect(Selection, _
Selection.SpecialCells(xlCellTypeConstants, 23)).ClearContents
End With
With MyData
'removes autofilter
If .FilterMode Then .ShowAllData
'filter the data based on selected date values
lastrow = Range("B" & Rows.Count).End(xlUp).Row
.Range("$B$2:$B$" & lastrow).AutoFilter Field:=2, Criteria1:= _
">=" & fromDate, Operator:=xlAnd, Criteria2:="<=" & toDate
.Range("G$2:G" & lastrow).AutoFilter Field:=7, Criteria1:=Vendedor
'copy the filtered data
.Range("$B$2:$B$30000").SpecialCells(xlCellTypeVisible).Copy
MyResults.Range("A17").PasteSpecial _
Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
'Informs the user, incase nothing has been parsed into the results table
If MyResults.Range("A17").Value = "" Then
MsgBox "Não há comissão neste período."
End If
'brings selection to the main cell
MyResults.Activate
MyResults.Range("B5").Select
End Sub
目的是根据输入的日期范围和推销员填充 A17:K。
任何帮助是极大的赞赏。
解决方案
最后,这是我解决它的方法,以防有人遇到这种问题:
Sub SelectDataBetweenTwoDates()
'declare variables
Dim fromDate, toDate As Date
Dim MyResults As Worksheet, MyData As Worksheet, MyDates As Worksheet
Set MyResults = Worksheets("Relatório de Comissão")
Set MyData = Worksheets("BI_Comissoes")
Set MyDates = Worksheets("Relatório de Comissão")
'attribute date values to variables
fromDate = MyDates.Range("B7").Value
toDate = MyDates.Range("B8").Value
Vendedor = MyResults.Range("B5").Value
'**THIS** is the part I commented out, since these are declared as Date, thus compatible with the data I'm trying to filter. Then, there was no need to convert it to string for filtering purposes.
'convert to text format to allow filtering
'fromDate = Format(fromDate, "dd/mm/yyyy")
'toDate = Format(toDate, "dd/mm/yyyy")
'MsgBox fromDate
'clear previous filtered data
With MyResults
If fromDate = 0 Or toDate = 0 Then
MsgBox "Informar o período!"
Exit Sub
Else
On Error Resume Next
Err.Number = 0
.Range("A$17:$K$20000").Select
Intersect(Selection, _
Selection.SpecialCells(xlCellTypeConstants, 23)).ClearContents
End If
End With
With MyData
'removes autofilter
If .FilterMode Then .ShowAllData
'filter the data based on selected date values
lastrow = Range("B" & Rows.Count).End(xlUp).Row
.Range("$B$2:$B$" & lastrow).AutoFilter Field:=2, Criteria1:= _
">=" & CDbl(fromDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(toDate)
.Range("G$2:G" & lastrow).AutoFilter Field:=7, Criteria1:=Vendedor
推荐阅读
- jquery - 链接 ckeditor 两个文本区域,用于从一个文本框到另一个文本框的文本流
- java - 从 firebase 中删除值时更新 Recycleview 适配器
- windows - 如何在 Windows 命令行中运行 shell 脚本?
- ruby-on-rails - 如何通过 https 和 heroku 上的 rails 服务云端资产?
- solr - 如何建模 solr 指数来对学生的技术技能进行排名?
- ios - 将 Google 地方信息中的城市和国家/地区从 RUS 语言更改为 ENG
- javascript - 为什么 JS 原型方法可以在没有 .cal() 或 .apply() 的情况下工作?
- python - 我有两个几乎相同的“for循环”函数,但只有一个有效。这是为什么?
- java - 如何使用 commons.cli 获取 Java 多行字符串
- excel - 我想根据特定单元格中存在的行数将数据从一张纸传输到另一张纸