繁体   English   中英

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

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

我正在处理一个电子表格,该电子表格具有需要基于三个不同单元格删除的数据。 这是我的目标:

如果A1 = 31 AND B1包含的日期大于C1中的日期,请删除整行。 我需要遍历整个数据集并检查每一行。 我将其删除(如果A1 = 31)并在工作表中循环,但似乎无法添加日期组件。

这是我的编码。 由于通常是复制和粘贴,因此可能有点混乱。 我不是一个非常有经验的编码人员,只要能正常工作,我也不在乎它的外观。 我也不使用海量数据集,因此速度不是问题。

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

仍不清楚

这不是有效的代码!

如果不打算在其他工作表上运行此代码,则应像使用Worksheets(2)一样将ActiveSheet更改为索引号。 最好使用工作表名称,最好使用代码名称。

您将必须定义我使用的“ OtherWorksheet”名称(或索引)并调整其列和行。 只有在使代码起作用后,才取消注释注释的部分的注释。

如果您提供所涉及的所有工作表的名称或索引以及确切的数据“ 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