删除包含日期和月份但忽略年份的行

时间:2018-12-24 11:45:11

标签: vba excel-vba

我想删除某个月初之前的所有行,例如,在一月1日之前的所有行都需要从下载的SAP数据中删除。当我想到不同的年份时,我只是在编写代码?

如何设置它以删除小于01/01而不是01/01/2018的内容?

Sub Filtration()
Dim Wb As Workbook
Dim Front As Worksheet
Dim Iws As Worksheet
Dim Ows As Worksheet
Dim x As Long
Dim Delete As Long
Dim L As String
Dim Answer As Integer
Dim Month As String

Set Wb = ThisWorkbook
Set Front = Wb.Worksheets("Front")
Set Iws = Wb.Worksheets("Input")
Set Ows = Wb.Worksheets("Output")

Iws.Columns("F").Replace What:=".", Replacement:="/", SearchOrder:=xlByColumns, MatchCase:=True

For Delete = Iws.Cells(Rows.Count, 6).End(xlUp).Row To 3 Step -1

With Iws.Cells(Delete, 6)
        If Month = "January" Then
        L = Left(Iws.Cells(Delete, 6), 5)
           If L < "01/01" Then Rows(Delete).EntireRow.Delete
        End If
 End With
Next Delete     

我知道上面的方法行不通,但是希望它将向您展示我要实现的目标,代码在每一行中查找时发现有一个日期,比如说31/12,然后删除该行。

1 个答案:

答案 0 :(得分:1)

使用DayMonth函数测试例如在4月10日之前删除:

If Day(Iws.Cells(Delete, 6)) < 10 And Month(Iws.Cells(Delete, 6)) < 4 Then Iws.Rows(Delete).EntireRow.Delete