简体   繁体   English

根据将单元格与用户输入的日期进行比较并跳过空白单元格来删除行

[英]Delete rows based on comparing cells to user entered date and skipping blank cells

I am looking for a way to remove rows based on the termination date of an employee. 我正在寻找一种根据员工的离职日期删除行的方法。 I don't want blank cells to be deleted because those employees are still active. 我不希望删除空白单元格,因为这些员工仍然活跃。 I have a text box that pops up and asks for the dates and then show the entered date. 我有一个弹出的文本框,询问日期,然后显示输入的日期。 Then it is supposed to search column G for any dates prior to the entered date and delete those rows, skipping any row that is blank. 然后,应该在G列中搜索输入日期之前的任何日期,然后删除这些行,并跳过任何空白的行。 I have been searching everywhere for a way to do this but I can't get the Macro to stop deleting every row but the headers. 我一直在到处寻找一种方法来执行此操作,但是我无法让宏停止删除标题以外的每一行。 The dates are in column G and it's about 46 rows but that can change. 日期在G列中,大约有46行,但是可以更改。

Sub DateSelectandClean()
    '
    ' DateSelectandClean Macro
    ' User enters date and spreadsheet deletes everything prior to that date, ignoring empty cells.

Application.ScreenUpdating = False

Dim W2Year As Date, N As Long
Dim dt As Date

W2Year = CDate(Application.InputBox(Prompt:="Please enter W2 Year as xx/xx/xxxx Date:", Type:=2))
MsgBox W2Year


N = Cells(Rows.Count, "G").End(xlUp).Row


For i = N To 2 Step -1
dt = Cells(i, 1).Value
    If (Cells(i, 1).Value <> "" And dt < W2Year) Then
        Cells(i, 1).EntireRow.Delete
    End If

Next i
Application.ScreenUpdating = True

End Sub

Sample Data 样本数据

Your code might be getting an issue in blank date. 您的代码可能在空白日期出现问题。
I separate the IF so that it won't continue the validation on date. 我将IF分开,以便它不会继续进行日期验证。
eg IF "" < #01/01/2017# 例如, IF "" < #01/01/2017#

Try this, not much changes though: 试试这个,虽然没有太大的变化:

Sub DateSelectandClean()

' DateSelectandClean Macro
' User enters date and spreadsheet deletes everything prior to that date, ignoring empty cells.

    Application.ScreenUpdating = False
    Dim dateCol, iRow
    Dim W2Year As Date, N As Long
    Dim dt As String

    W2Year = CDate(Application.InputBox(Prompt:="Please enter W2 Year as dd/mm/yyyy Date:", Type:=2))
    MsgBox W2Year

    N = Cells(Rows.Count, "G").End(xlUp).Row
    dateCol = 7
    For iRow = N To 2 Step -1
        dt = Cells(iRow, dateCol).Value
        If (Cells(iRow, dateCol).Value <> "") Then
            If (CDate(dt) < CDate(W2Year)) Then
                Cells(iRow, dateCol).EntireRow.Delete
            End If
        End If
    Next

    Application.ScreenUpdating = True

End Sub

The main issue is you're checking the "A" column for your date info, and deleting based on that. 主要问题是您要检查“ A”列中的日期信息,并根据该列进行删除。 If your dates are in "G", you should check Cells(x,7) , not Cells(x,1) . 如果日期在“ G”中,则应检查Cells(x,7) ,而不是Cells(x,1)

Sub DateSelectandClean()
'
' DateSelectandClean Macro
' User enters date and spreadsheet deletes everything prior to that date, ignoring empty cells.

Application.ScreenUpdating = False

Dim W2Year As Date, lastRow As Long, i As Long, dateCol As Long
Dim dt      As Date

dateCol = 7                  ' for column G

Do While W2Year = "00:00:00"
    W2Year = Format(Application.InputBox(Prompt:="Please enter W2 Year as xx/xx/xxxx Date:", Type:=2), "mm/dd/yyyy")
    MsgBox W2Year
Loop

lastRow = Cells(Rows.Count, dateCol).End(xlUp).Row

For i = lastRow To 2 Step -1
    'If Cells(i, dateCol).Value <> "" Then
    If IsDate(Cells(i,dateCol)) Then
        dt = CDate(Format(Cells(i, dateCol).Value, "mm/dd/yyyy"))
        If dt <= W2Year Then
            Cells(i, dateCol).EntireRow.Delete
        End If
    End If
Next i
Application.ScreenUpdating = True
End Sub

I also change the variables from Date to String which allows a little bit of error catching when the user inputs info. 我还将变量从Date更改为String ,这在用户输入信息时允许捕获一些错误。 You can edit that back if you wish, I was just thinking of a time when someone puts in "wrong" or incorrectly formatted info. 您可以根据需要将其编辑回去,我只是想一个人输入“错误”或格式错误的信息的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM