我希望做一个IF THEN声明,这将有助于我在U列中添加日期。但是,我不知道该怎么做。
Sub()
Dim rng1 As Range
Set rng1 = Selection.SpecialCells("U").xlBlanks
If Not rng1 Is Nothing Then rng1.Value = Format(Now(), "mm-dd-yyyy")
End Sub
当我运行此代码时,我收到运行时错误“1004”。有人可以帮我这个吗?
答案 0 :(得分:0)
首先,您要查找实际拥有数据的单元格范围,因为您不想检查整行本身为空的列U行。因此,假设可以在A列中找到最后一行数据,您可以使用它来设置搜索范围。
With Worksheets("Sheet1") 'qualify sheet, change name as needed
Dim lRow as Long
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
然后,您可以将搜索范围设置为从第1行到lRow
,假设数据从A1开始从左到右,从上到下连续的范围内。 (On Error语句是必需的,因为如果范围中不存在空白单元格,Excel将引发错误。)
Dim rngSearch as Range
On Error Resume Next
Set rngSearch = .Range("U1:U" & lRow).SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
如果设置了范围,那么您可以立即在整个范围内输入值(您在上面的代码中已经存在)。
If Not rngSearch is Nothing Then rngSearch.Value = Format(Now(), "mm-dd-yyyy")
End With