我想按日期筛选行。
我希望保留今天和一周前的行的日期。
我已经从其他来源修改了下面的代码,但是不起作用。
我的问题是如何修改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
发布于 2018-04-18 16:06:46
我的答案将集中在日期测试上。我还没有看过你代码的其他部分。替换
(xVal >= Date - 7)
使用
(xVal >= Now - 7)
你就会更近一步。这里有一些小问题,因为Now将为您提供今天的日期和时间,这使得边缘情况有点棘手。还有--看一下DateDiff函数
https://stackoverflow.com/questions/49893800
复制相似问题