我想按日期过滤行。
我想保留今天和一周之前的行。
我已修改其他来源的以下代码,但不起作用。
我的问题是如何修改代码7天或15天,如何在同一工作表中删除除过滤天数以外的行而不是复制到另一张表?
我可以看到问题是If TypeName(xVal) = "Date" And (xVal >= Date - 7) Then
,但我无法解决。
请检查下面的代码:
Sub CopyRowWithCurrentDate()
Dim xRgS As Range, xRgD As Range, xCell As Range
Dim I As Long, xCol As Long, J As Long
Dim xVal As Variant
On Error Resume Next
Set xRgS = Application.InputBox("Select the Date Column:", "Filter On Date", Selection.Address, , , , , 8)
If xRgS Is Nothing Then Exit Sub
Set xRgD = Application.InputBox("Select a destination cell:", "Filter On Date", , , , , , 8)
If xRgD Is Nothing Then Exit Sub
xCol = xRgS.Rows.Count
Set xRgS = xRgS(1)
Application.CutCopyMode = False
J = 0
For I = 1 To xCol
Set xCell = xRgS.Offset(I - 1, 0)
xVal = xCell.Value
If TypeName(xVal) = "Date" And (xVal >= Date - 7) Then
xCell.EntireRow.Copy xRgD.Offset(J, 0)
J = J + 1
End If
Next
Application.CutCopyMode = True
End Sub
答案 0 :(得分:0)
我的回答将集中在日期测试上。我还没有查看代码的其他部分。 替换
(xVal >= Date - 7)
与
(xVal >= Now - 7)
你会更近一步。这里有一些小问题,因为现在会给你今天的约会时间,使边缘案件有点棘手。另外 - 看一下DateDiff函数