0

我只在有一个日期过去的情况下才尝试删除一行,但如果它在未来或空白则不会。现在,如果没有空白,我很难让代码不删除该行,因为我想保留没有日期的行。

Sub deleteSpudedWells()
Dim lastRow As Integer
Dim firstRow As Integer
Dim ctr As Integer

Dim currentCell As Range
Dim valueOfDColumn
Dim NoNSpudedWells As Boolean



 lastRow = 300
 firstRow = 10

 Application.ScreenUpdating = False
 With Sheets("NDIC Renewals")
     For ctr = lastRow To firstRow Step -1
         Set currentCell = .Cells(ctr, 4)
         valueOfDColumn = currentCell.Value
         NoNSpudedWells = valueOfDColumn >= Date


         If Not NoNSpudedWells Then
             Debug.Print "deleting row of cell " + currentCell.Address
             currentCell.EntireRow.Delete
         End If
     Next
 End With
 Application.ScreenUpdating = True
 End Sub
4

3 回答 3

1

为您的 if 添加另一个条件:

If Not NoNSpudedWells AND valueOfDColumn <> 0 Then

如果currentCell为空,它的值将 = 0。

于 2012-12-20T22:00:12.020 回答
1

试试这个:

    For ctr = lastRow To firstRow Step -1
         Set currentCell = .Cells(ctr, 4)
         valueOfDColumn = currentCell.Value               

         If NOT (valueOfDColumn >= Date or currentCell.Value = "") Then
             Debug.Print "deleting row of cell " + currentCell.Address
             currentCell.EntireRow.Delete
         End If
     Next

顺便说一句 ,最好声明您的valueOfDColumnasDate而不是将其保留为默认值variant:)

于 2012-12-20T22:01:51.390 回答
1

感谢 BonCodigo 它给了我一个想法,我能够调整代码工作。需要阅读此内容以使代码正常运行valueOfDColumn = ""

 Sub deleteSpudedWells()
     Dim lastRow As Integer
     Dim firstRow As Integer
     Dim ctr As Integer

     Dim currentCell As Range
     Dim valueOfDColumn
     Dim NoNSpudedWells As Boolean

     lastRow = 300
     firstRow = 10

     Application.ScreenUpdating = False
     With Sheets("NDIC Renewals")
         For ctr = lastRow To firstRow Step -1
             Set currentCell = .Cells(ctr, 4)
             valueOfDColumn = currentCell.Value
             NoNSpudedWells = valueOfDColumn >= Date Or valueOfDColumn = ""

             If Not NoNSpudedWells Then
                 Debug.Print "deleting row of cell " + currentCell.Address
                 currentCell.EntireRow.Delete
             End If
         Next
     End With
     Application.ScreenUpdating = True
 End Sub
于 2012-12-20T22:16:04.963 回答