简体   繁体   English

根据两列中的值删除行

[英]Delete Rows based on Values in Two Columns

I have an excel sheet that I need to delete rows based on the values from two different cells.我有一个 Excel 工作表,我需要根据两个不同单元格的值删除行。

The initial excel sheet is created from a report that will populate the data from the previous month up until the current day.初始 Excel 工作表是根据报告创建的,该报告将填充上个月到当天的数据。 Depending on the day the report is ran I could have an extra three days or four days worth of data that I do not need.根据报告运行的日期,我可能会有额外三天或四天的数据,这些数据是我不需要的。 I would like to delete the rows that I do not need based on the month and time columns.我想根据月份和时间列删除不需要的行。

Simplified Excel Sheet简化的 Excel 工作表
在此处输入图像描述

I would like to delete everything past the last day of the month except for the initial data on the 1st at 0:00.我想删除该月最后一天之后的所有内容,但 1 日 0:00 的初始数据除外。 In this example I would like to delete everything below row #4.在此示例中,我想删除第 4 行下方的所有内容。

Any help is greatly appreciated.任何帮助是极大的赞赏。

So far I have tried the following and I can filter the data on the date column, but I have not been successful at combining the filter for the second column.到目前为止,我已经尝试了以下方法,我可以过滤日期列上的数据,但我没有成功地为第二列组合过滤器。

Sub Delete_Row_Cell_Contains_Text()
    Dim Row As Long
    Dim i As Long
    Row = 15
    For i = Row To 1 Step -1
        If Cells(i, 1) = "12" Then
            Rows(i).Delete
        End If
    Next
End Sub

Try something like this:尝试这样的事情:

Sub Delete_Row_Cell_Contains_Text()
    
    Dim i As Long, ws As Worksheet, currMonth As Long, dt As Date
    Dim keepDateTime
    
    currMonth = Month(Date)
    keepDateTime = DateSerial(Year(Date), currMonth, 1) 'date/time row to keep
    
    Set ws = ActiveSheet
    For i = ws.Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1 'from last date up...
        dt = ws.Cells(i, 1).Value
        If Month(dt) <> currMonth - 1 Then 'if not this month-1
            'if not the first of this month at midnight
            If dt + ws.Cells(i, 2).Value <> keepDateTime Then
                ws.Rows(i).Delete
            End If
        End If
    Next
End Sub

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

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