【发布时间】:2021-01-23 14:03:44
【问题描述】:
我对 VBA/宏很陌生...我希望宏过滤名为“ABC”的列并删除所有未来日期的数据。例如,今天的日期 10/8/2020 哪个宏应该检查并删除所有具有未来日期的数据。示例 在下面的 excel 屏幕宏中,应删除日期为 10\10\2020 的数据,该数据大于今天的日期,并保留日期为 10\04\2020 的数据,该数据已过去。
我尝试了多种编码方式,但都没有成功。以下是我尝试过的最新代码,但失败了。
Private Sub CommandButton5_Click()
Dim row As Long
Dim lastrow As Long
Dim mindate As Date
Dim maxdate As Date
Dim st As Worksheet
'Column Name
cons = "Vessel Estimated Time of Departure"
st = Worksheet("POL")
mindate = CDate(Cells(1, 10))
maxdate = CDate(Cells(1, 12))
ActiveWorkbook.Worksheets("POL").lastrow = Cells(Rows.Count, "cons").End(xlUp).row
row = 1 'Set here the starting row for checking transaction dates
Do While row <= lastrow
transdate = CDate(Cells(row, 3))
If transdate < mindate Or transdate > maxdate Then
Rows(row).EntireRow.Delete
Else
row = row + 1
End If
Loop
End Sub
我想要的只是“船舶预计出发时间”列上的宏过滤器,并默认从当前系统日期中删除所有具有未来日期的数据。
【问题讨论】:
-
使用
Range.AutoFilter而不是循环。您可以轻松应用时间过滤器。过滤数据后,您可以删除可见行。 -
一些新东西我该怎么做
-
使用宏记录器或在 SO 上搜索类似问题。有很多。