Excel 在列中的日期之间自动筛选+;未填充目标的另一个条件

Excel 在列中的日期之间自动筛选+;未填充目标的另一个条件,excel,vba,Excel,Vba,我有一个数据表(表名为BI_Comissoes),其中有日期列和销售人员列。然后,我得到了目的地表,它作为一个报告,根据B7和B8上输入的日期范围和B5上输入的销售人员姓名填充。 运行代码后,不会填充目标工作表,但数据工作表会正常过滤。 这个代码以前是有效的,但我想我在某个地方把它弄乱了,我的神经元似乎被烧坏了,因为我找不到问题出在哪里 我尝试过很多约会方式,但似乎都不管用。请参考,数据表上的日期为dd/mm/yyyy Sub SelectDataBetweenTwoDates() 'd

我有一个数据表(表名为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
Sub-selectDatabetweetwodates()
'声明变量
Dim fromDate,toDate As Date
将MyResults设置为工作表、MyData设置为工作表、MyDates设置为工作表
Set MyResults=工作表(“社区关系”)
设置MyData=工作表(“BI_Comissos”)
设置MyDates=工作表(“社区关系”)
"明确以往的结果,
“MyResults.Cells.Clear”
'将日期值属性设置为变量
fromDate=MyDates.Range(“B7”).值
toDate=MyDates.Range(“B8”).值
Vendedor=MyResults.Range(“B5”).值
'转换为文本格式以允许筛选
fromDate=格式(fromDate,“dd-mmm-yyyy”)
toDate=格式(toDate,“dd-mmm-yyyy”)
'清除以前过滤的数据
用我的结果
'如果fromDate=“”或toDate=“”,则
“MsgBox”告密者o período
“出口接头
“否则,请继续
出错时继续下一步
错误号=0
.范围(“A$17:$K$20000”)。选择
相交(选择)_
Selection.SpecialCells(xlCellTypeConstants,23)).ClearContent
以
使用MyData
'删除自动筛选
If.FilterMode Then.ShowAllData
'根据选定的日期值筛选数据
lastrow=范围(“B”和Rows.Count).End(xlUp).Row
.Range($B$2:$B$”&lastrow)。自动筛选字段:=2,标准1=_

“>=”&fromDate,运算符:=xlAnd,准则2:=”您需要在
double
中转换日期,以避免此类错误:
Autofilter()
使用通用格式
yyyy-mm-dd
。这里您的格式非常不同,因此VBA无法解释您的日期(例如,1月2日将被读取为2月1日)


Criteria1:=“>=”&CDbl(fromDate),操作符:=xlAnd,Criteria2:=“=”&CDbl(fromDate),操作符:=xlAnd,Criteria2:=”最后,如果有人遇到这种问题,我是如何解决的:

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
Sub-selectDatabetweetwodates()
'声明变量
Dim fromDate,toDate As Date
将MyResults设置为工作表、MyData设置为工作表、MyDates设置为工作表
Set MyResults=工作表(“社区关系”)
设置MyData=工作表(“BI_Comissos”)
设置MyDates=工作表(“社区关系”)
'将日期值属性设置为变量
fromDate=MyDates.Range(“B7”).值
toDate=MyDates.Range(“B8”).值
Vendedor=MyResults.Range(“B5”).值
“**这**是我注释掉的部分,因为它们被声明为日期,因此与我试图筛选的数据兼容。然后,无需将其转换为字符串进行过滤。
'转换为文本格式以允许筛选
'fromDate=格式(fromDate,“dd/mm/yyyy”)
'toDate=格式(toDate,“dd/mm/yyyy”)
'MsgBox fromDate
'清除以前过滤的数据
用我的结果
如果fromDate=0或toDate=0,则
MsgBox“告密者o período!”
出口接头
其他的
出错时继续下一步
错误号=0
.范围(“A$17:$K$20000”)。选择
相交(选择)_
Selection.SpecialCells(xlCellTypeConstants,23)).ClearContent
如果结束
以
使用MyData
'删除自动筛选
If.FilterMode Then.ShowAllData
'根据选定的日期值筛选数据
lastrow=范围(“B”和Rows.Count).End(xlUp).Row
.Range($B$2:$B$”&lastrow)。自动筛选字段:=2,标准1=_

“>=”&CDbl(fromDate),运算符:=xlAnd,标准2:=“你好@Dorian!我试过了,但奇怪的是,它现在给我的行中包含的日期超出了在B7和B8上键入的范围。我正在使用数据格式来检查根本原因可能在哪里。。。你知道我该去哪里吗?谢谢@AntonioSantos尝试调试。将日期打印为长格式,并查看vba是否将日期读取为例外。我相信我将不得不接受我的解决方案,因为排除转换是解决问题的方法。你不觉得吗?:)您是否有必要就您的信仰进行必要的转换发表评论?干杯
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

   '**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-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:= _
                ">=" & CDbl(fromDate), Operator:=xlAnd, Criteria2:="<=" & CDbl(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
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