我只是在今天有一个日期时才尝试删除一行,但是如果它是将来或空白则不会。现在我很难让代码在没有空白的情况下不删除行,因为我想保留没有日期的行。
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
答案 0 :(得分:1)
为if:
添加另一个条件If Not NoNSpudedWells AND valueOfDColumn <> 0 Then
如果currentCell
为空,则其值为=。
答案 1 :(得分: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
<强>顺便说一句强>
最好将valueOfDColumn
声明为Date
,而不是将其保留为默认variant
:)
答案 2 :(得分: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