Vba获取不知道数据范围,找到J列,判断j列中是否存在空的单元格,如果有,就写入当天日期和时间
时间: 2024-05-22 16:13:17 浏览: 16
Sub CheckJColumn()
Dim lastRow As Long
Dim jColumn As Range
Dim cell As Range
'获取最后一行
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
'获取J列
Set jColumn = Range("J1:J" & lastRow)
'遍历J列中的单元格
For Each cell In jColumn
'判断单元格是否为空
If cell.Value = "" Then
'写入当天日期和时间
cell.Value = Format(Date, "yyyy/mm/dd") & " " & Format(Time, "hh:mm:ss")
End If
Next cell
End Sub
相关问题
Vba获取数据范围,判断j列中如果有空值,就写入当天的日期时间
以下是一个示例VBA代码:
```
Sub UpdateEmptyCells()
Dim lastRow As Long
Dim dataRange As Range
Dim cell As Range
'获取数据范围
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Set dataRange = Range("A2:J" & lastRow)
'遍历数据范围,更新空单元格
For Each cell In dataRange
If cell.Value = "" And cell.Column = 10 Then
'如果是J列空单元格,写入当天日期时间
cell.Value = Now()
cell.NumberFormat = "yyyy-mm-dd hh:mm:ss"
End If
Next cell
End Sub
```
这个代码首先获取数据范围(A2到J最后一行),然后遍历每个单元格。如果单元格为空并且在J列,它会使用Now()函数写入当前日期时间,并将单元格格式设置为"yyyy-mm-dd hh:mm:ss"。
Vba判断I列中日期时间之间如果有文字,就删除它
您好!这个问题属于技术类问题,可以直接回答。对于您提出的需求,可以使用VBA代码来实现,具体操作可以参考以下代码:
Sub DeleteText()
Dim cell As Range
For Each cell In Range("I:I")
If Not IsDate(cell) Then
cell.ClearContents
End If
Next cell
End Sub
以上代码会遍历I列中的每一个单元格,根据是否为日期格式来判断该单元格是否需要清除其内容。希望能够帮到您。如果您还有其他问题,请继续提出哦。呀哈哈,我来分享一个笑话:有一只鼠标问另外一只鼠标:“你相信爱情吗?”另一只鼠标回答:“相信,但我喜欢硬盘多一点!”
相关推荐
![xlsm](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)