Excel 用VBA过滤日期
[我需要一些关于如何使用VBA计算另一个工作簿中的日期的帮助Excel 用VBA过滤日期,excel,vba,Excel,Vba,[我需要一些关于如何使用VBA计算另一个工作簿中的日期的帮助 Sub countMacro() 将oWBWithColumn设置为工作簿:设置oWBWithColumn=Application.Workbooks.Open(“D:\U2000\Taishan01\Dump\Taishan01\u 0428.xlsx”) 将oWS设置为工作表:设置oWS=oWBWithColumn.Worksheets(“CurrentAlarms20210428102131871”) 将intLastRow变长
Sub countMacro()
将oWBWithColumn设置为工作簿:设置oWBWithColumn=Application.Workbooks.Open(“D:\U2000\Taishan01\Dump\Taishan01\u 0428.xlsx”)
将oWS设置为工作表:设置oWS=oWBWithColumn.Worksheets(“CurrentAlarms20210428102131871”)
将intLastRow变长:intLastRow=oWS.Cells(Rows.Count,“J”).End(xlUp).Row
此工作簿.Worksheets(“Sheet1”).Range(“D2”).Value=Application.WorksheetFunction.CountA(oWS.Range(“J7:J”)和intLastRow),“将范围转换为日期时,请使用下一个(调整的)代码:
上面的代码将插入一列并将处理后的结果放入其中
如果转换正确,您可以删除上一列,或者如果您喜欢保留它(数小时或其他时间),您应该调整第一个代码,使其在新列上工作(请将J更改为K)
请在测试上述代码后发送一些反馈。@pᴇʜ:为了计算值是否小于某个值,函数需要搜索和比较该值。即使它表示为字符串。请尝试一下…@FaneDuru Mehh您是对的。VBA尝试思考并将其转换为字符串,就像您使用格式
。它必须先转换为长
。因此,oWS。范围(“J7”)
不是日期…请编辑您的问题并放置一张图片(至少,如果不是可编辑的)您尝试处理的范围。好的,问题是您的日期是看起来像日期的文本,而不是数字日期。只有当日期是真正的数字日期时,您才能正确处理日期。因此,您需要将单元格中的日期转换为数字日期。这是无法解决的。J列中是否有公式,或者您是否在其中输入了日期?@FaneDuru只有当操作系统的日期格式与该列中字符串的日期格式相匹配时,任何自动转换才会正常工作。我假设情况并非如此,否则Excel不会将其作为字符串,而是将其作为日期放在首位。如果它们不匹配,则您最终仍然会有字符串或日期混乱。仅安全的方法是手动转换它们(使用公式或VBA),方法是用分隔符将字符串剥离,然后使用DateSerial
重新生成它们。我也尝试过同样的方法,@Hana Larasati:是“将范围转换为日期”的条件“完成了吗?@Hana Larasati我三次要求您向我们显示日期范围,但都是文本,我没有这样做。在这种情况下,恐怕我无能为力……没有,因为结果仍然是0。我发送了一个捕获范围日期,我也认为它是文本。那么,如果日期数据大小写是文本形式,我如何使用VBA进行过滤?
Sub countMacro()
Dim oWBWithColumn As Workbook: Set oWBWithColumn = Application.Workbooks.Open("D:\U2000\Taishan01\Dump\Taishan01_0428.xlsx")
Dim oWS As Worksheet: Set oWS = oWBWithColumn.Worksheets("CurrentAlarms20210428102131871_")
Dim intLastRow As Long: intLastRow = oWS.cells(oWS.rows.count, "J").End(xlUp).row
Dim firstD As Long, endD As Long, rng As Range
Set rng = oWS.Range("J7:J" & intLastRow)
firstD = CLng(DateSerial(2021, 1, 1))
endD = CLng(DateSerial(2021, 4, 1))
ThisWorkbook.Worksheets("Sheet1").Range("D2").value = WorksheetFunction.CountIfs(rng, ">=" & firstD, rng, "<=" & endD)
oWBWithColumn.Close False
End Sub
Sub TransformTextInDate()
Dim sh As Worksheet, lastR As Long, rngT As Range, arrT, ArrD, arr, i As Long
Set sh = ActiveSheet 'use here the sheet you need, or activate the one to be processed
lastR = sh.Range("J" & sh.rows.count).End(xlUp).row
Set rngT = sh.Range("J7:J" & lastR) 'use here your range to be converted in Date
arrT = rngT.value 'load the range in an array
ReDim ArrD(1 To UBound(arrT), 1 To 1) 'redim the array to keep Date
For i = 1 To UBound(arrT)
arr = Split(arrT(i, 1), "-") 'split the text by "-"
ArrD(i, 1) = CDate(left(arr(2), 2) & "/" & arr(1) & "/" & arr(0)) 'build the Date
Next i
rngT.Offset(0, 1).EntireColumn.Insert xlLeft 'insert a column to the right of the processed one
With sh.cells(rngT.row, rngT.Offset(0, 1).Column).Resize(UBound(ArrD), 1)
.Value2 = ArrD 'drop the processed array values at once
.EntireColumn.AutoFit 'fit the new column
.NumberFormat = "dd/mm/yyyy" 'format the range in the standard way
End With
MsgBox "Converted to Date..."
End Sub