简体   繁体   English

VBA帮助-如果一个单元格包含特定值而另一个单元格具有日期? 而不是另一个单元格-删除整行

[英]VBA Help- If one cell contains a specific value AND another cell has a date ? than another cell- delete entire row

I am working on a spreadsheet that has data I need to remove based on three different cells. 我正在处理一个电子表格,该电子表格具有需要基于三个不同单元格删除的数据。 Here is my goal: 这是我的目标:

If A1=31 AND B1 contains a date > than the date in C1, delete the entire row. 如果A1 = 31 AND B1包含的日期大于C1中的日期,请删除整行。 I need to loop through the full data set and check each row. 我需要遍历整个数据集并检查每一行。 I have it removing the row if A1=31 and looping through the worksheet, but can't seem to add the date component. 我将其删除(如果A1 = 31)并在工作表中循环,但似乎无法添加日期组件。

Here is my coding. 这是我的编码。 It is probably kind of messy since mostly copied and pasted. 由于通常是复制和粘贴,因此可能有点混乱。 I am not an a very experienced coder and don't really care what it looks like as long as it works. 我不是一个非常有经验的编码人员,只要能正常工作,我也不在乎它的外观。 I am also not working with massive datasets so speed isn't an issue. 我也不使用海量数据集,因此速度不是问题。

Sub Remove_Future_Dates()
    Dim Firstrow As Long
    Dim LastRow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

Firstrow = ThisWorkbook.Worksheets(2).UsedRange.Cells(1).Row
LastRow = ThisWorkbook.Worksheets(2).Cells(ThisWorkbook.Worksheets(2).Rows.Count, "A").End(xlUp).Row

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        '.ScreenUpdating = False
    End With
    With ActiveSheet
        .Select
        Firstrow = .UsedRange.Cells(1).Row
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For Lrow = LastRow To Firstrow Step -1

            With .Cells(Lrow, "A")

                If Not IsError(.Value) Then

                    If .Value = "31" Then .EntireRow.Delete
                End If
                End With

        Next Lrow

    End With

End Sub

Still Unclear 仍不清楚

This is not yet a working code! 这不是有效的代码!

If you are not going to run this code on different sheets you should change ActiveSheet to the index number like you did with Worksheets(2). 如果不打算在其他工作表上运行此代码,则应像使用Worksheets(2)一样将ActiveSheet更改为索引号。 It is preferred to use worksheet names and best to use code names though. 最好使用工作表名称,最好使用代码名称。

You will have to define the 'OtherWorksheet' name (or index) I used and adjust its column and row. 您将必须定义我使用的“ OtherWorksheet”名称(或索引)并调整其列和行。 Uncomment the commented parts only after you got the code working. 只有在使代码起作用后,才取消注释注释的部分的注释。

I make this code working, if you provide the names or the indexes of all the worksheets envolved and an exact 'series of 3' of data eg A1, B1 on Sheet1, worksheet(1), Worksheet("Master") or ActiveSheet and C2 on Sheet3, Worksheet(3) or Worksheet("Slave"). 如果您提供所涉及的所有工作表的名称或索引以及确切的数据“ 3系列”,例如A1,Sheet1上的B1,worksheet(1),Worksheet(“ Master”)或ActiveSheet,则可以使此代码正常工作Sheet3,Worksheet(3)或Worksheet(“ Slave”)上的C2。

Option Explicit

Sub Remove_Future_Dates()

    Dim Firstrow As Long
    Dim LastRow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

'    With Application
'        CalcMode = .Calculation
'        .Calculation = xlCalculationManual
'        .ScreenUpdating = False
'    End With

    On Error GoTo SafeExit

    With ThisWorkbook.Worksheets(2)
        Firstrow = .UsedRange.Cells(1).Row
        LastRow = .Worksheets(2).Cells(Rows.Count, "A").End(xlUp).Row
    End With

    With ActiveSheet
        For Lrow = LastRow To Firstrow Step -1
            With .Cells(Lrow, "A")
                If Not IsError(.Value) Then
                    If .Value = "31" _
                        And .Offset(0, 1) > _
                        ThisWorkbook.Worksheets("OtherWorksheet") _
                            .Cells(Lrow + 1, "C") _
                        Then .EntireRow.Delete
                End If
            End With
        Next Lrow
    End With

SafeExit:

'    With Application
'        CalcMode = .Calculation
'        .Calculation = xlCalculationManual
'        .ScreenUpdating = True
'    End With

End Sub

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

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