删除基于另一个日期单元格的单元格



在Excel VBA中工作。如果另一个单元格中有日期,我正试图通过VBA删除该单元格。或者换一种说法,我试图删除一个单元格,如果另一个单元格中有任何东西。(因为它要么是日期,要么不是。(

这是我的代码——我只是不知道如何识别手机中的任何日期。

Sub Upload1ClearADP()
Dim LastRow As Long, x As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For x = 2 To LastRow
If Cells(x, "G").Value = "Date" Then
Cells(x, "U").ClearContents
End If
Next x
End Sub

您当前正在检查字符串Date,而不是实际日期。

这是你写的代码,用来检查它是日期还是空的:

Sub Upload1ClearADP()
Dim LastRow As Long, x As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For x = 2 To LastRow
If IsDate(Cells(x, "G").Value) or Cells(x, "G") <> "" Then
Cells(x, "U").ClearContents
End If
Next x
End Sub

编辑:正如@Harun24HR在评论中指出的,IsDate()是不必要的,因为你要检查单元格是否为空(<> ""(。我只是想把它放在那里介绍IsDate()函数。

编辑2:您也可以使用SpecialCells()在一行中进行清除:

Sub Upload1ClearADP()
Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Dim dataRng As Range
Set dataRng = Range(Cells(2, "G"), Cells(LastRow, "G"))
' Use 14 because it's 14 columns to the right from
' Column G to U
dataRng.SpecialCells(xlCellTypeConstants).Offset(0, 14).ClearContents
' If you have formulas *and* constants in column G, use:
' Union(dataRng.SpecialCells(xlCellTypeConstants), _
'     dataRng.SpecialCells(xlCellTypeFormulas)).Offset(0,14).ClearContents
End Sub

最新更新