简体   繁体   English

使用 vba 的 excel 范围内的日期过滤器不起作用

[英]date filter on excel range using vba does not work

I am trying to filter between a date range in excel using vba. It is not working.我正在尝试使用 vba 在 excel 的日期范围之间进行过滤。它不起作用。

However when I perform a manual filter, it works.但是,当我执行手动过滤器时,它会起作用。 So I recorded the macro and applied the same code.所以我录制了宏并应用了相同的代码。 Again, it did not work.同样,它没有用。

Date format in file is as below:文件中的日期格式如下:

在此处输入图像描述

Let's say I am trying to filter out 01 to 31 July 2021.假设我正在尝试过滤掉 2021 年 7 月 1 日至 31 日。

Here's my code that does not work: Date in in A column.这是我的代码不起作用:A 列中的日期。

startDT = ActiveSheet.txtStartDT.Text   '01-Jul-2021
endDT = ActiveSheet.txtEndDT.Text    '31-Jul-2021

For Each srcWsht In srcWbk.Worksheets
            lastSrcRow = srcWsht.Range("A1").End(xlDown).Row
                        
            srcWsht.Range("A2:A" & lastSrcRow).Replace What:="'", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
            srcWsht.Range("A2:A" & lastSrcRow).NumberFormat = "dd-MM-yyyy"
            
            srcWsht.Range("A1:O" & lastSrcRow).AutoFilter Field:=1, Criteria1:=">=" & CDate(startDT), Operator:=xlAnd, Criteria2:="<=" & CDate(endDT), visibledropdown:=True
                 
            
            srcWsht.Range("A2:O" & lastSrcRow).SpecialCells(xlCellTypeVisible).Copy
            
            wshtData.Range("A" & lastDataRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            lastDataRow = wshtData.Range("A2").End(xlDown).Row + 1
            
Next srcWsht

Your filter needs a numeric value to filter.您的过滤器需要一个数值来过滤。 But if you concatenate a String with a Date as you do in ">=" & CDate(startDT) it will implicitly convert your numeric date into a string to concatenate it.但是,如果您像在">=" & CDate(startDT)中那样将StringDate连接起来,它将隐式地将您的数字日期转换为字符串以连接它。 Depending on your default date format set in your operating system (mine is yyyy-mm-dd ) this will result in something like ">=2021-07-01" which is not considered as a date but as a formula to calculate and it will subtratct 07 and 01 from 2021 and your filter does >=2013 which is not what you want.根据您在操作系统中设置的默认日期格式(我的是yyyy-mm-dd ),这将导致类似">=2021-07-01"的结果,它不被视为日期,而是被视为计算公式,并且它将从2021减去0701并且您的过滤器>=2013这不是您想要的。

So by converting your Date into a Double like CDbl(CDate(startDT)) you convert the date 2021-07-01 into 44378 which is the actual numeric value of the date that is stored in your cells.因此,通过将Date转换为CDbl(CDate(startDT))之类的Double ,您可以将日期2021-07-01转换为44378 ,这是存储在单元格中的日期的实际数值。 Now your filter does >=44378 and this will work现在你的过滤器确实>=44378并且这将起作用

srcWsht.Range("A1:O" & lastSrcRow).AutoFilter Field:=1, Criteria1:=">=" & CDbl(CDate(startDT)), Operator:=xlAnd, Criteria2:="<=" & CDbl(CDate(endDT)), visibledropdown:=True

Note your values are Strings (text that look like dates) if the begin with ' .请注意,如果以'开头,您的值是Strings (看起来像日期的文本)。 If you now remove that ' Excel will convert them to numeric dates (so that you can calculate and filter them).如果您现在删除' Excel 会将它们转换为数字日期(以便您可以计算和过滤它们)。 But Excel guesses the format.但是Excel猜到了格式。 So in case they are dd-MMM-yyyy Excel has it pretty clear which format it is and will guess correctly but be carefull with strings like 01/05/2022 where Excel cannot know if this is dd/mm/yyyy or mm/dd/yyyy .因此,如果它们是dd-MMM-yyyy Excel 非常清楚它是哪种格式并且会正确猜测但要小心像 01/05/2022 这样的字符串,其中01/05/2022不知道这是dd/mm/yyyy还是mm/dd/yyyy If you let Excel convert that it is a 50/50 chance to get it wrong or right!如果您让 Excel 转换它,则有 50/50 的机会弄错或正确!

If that is the case you need to split your strings that look like dates apart into day, month and year, and use DateSerial(Year, Month, Day) to convert it into a real numeric date.如果是这种情况,您需要将看起来像日期的字符串拆分为日、月和年,并使用DateSerial(Year, Month, Day)将其转换为实际数字日期。 This way you define which format dd/mm/yyyy or mm/dd/yyyy your strings are and you do not let Excel guess (and maybe come up with the wrong one).通过这种方式,您可以定义字符串的格式为dd/mm/yyyymm/dd/yyyy ,并且您不会让 Excel 猜测(并且可能会猜错)。

You might also need to do that if your month names are english 01-May-2021 but you work in another localized system (like I do in German).如果您的月份名称是英语01-May-2021但您在另一个本地化系统中工作(就像我在德语中所做的那样),您可能还需要这样做。 Because the German Excel has no idea how to convert May into a month because in German it is Mai .因为德语 Excel 不知道如何将May转换为一个月,因为在德语中是Mai So Excel cannot convert that automatically and you need to split and convert that with your own function too (or it will only work in English Excels).所以 Excel 不能自动转换,你也需要用你自己的 function 拆分和转换它(否则它只能在英文 Excel 中工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM