仅从Excel的列中选择日期单元格

时间:2019-04-18 05:57:58

标签: excel vba excel-formula

我正在使用Excel工作表,该表中有多个日期和其他数字。我试图仅选择日期,然后将其复制到下一列。我尝试使用“查找并替换”选项,但不起作用,我使用了KUTOOLS脚本,但选择了值的范围。因此值落在两个不同值的相同范围内,因此无法区分。如何在Excel中选择仅单元格日期。

Sub FindReplace()
'Update 20150423
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    If Rng.Value > 500 Then
        Rng.Value = 0
    End If
Next
End Sub

下面是Excel工作表

[1]:

2 个答案:

答案 0 :(得分:0)

如果值是DATE格式,则可以使用cell.Value(不是Value2)上的IsDate函数进行过滤。

Dim rng As Range
Set rng = Worksheets(1).Range("A2", "A5")
Dim cell As Range
For Each cell In rng.Cells
  With cell
    If IsDate(cell.Value) Then
      Debug.Print cell.Value
    End If
  End With
Next cell

答案 1 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    Dim LastRow As Long, i As Long

    With ThisWorkbook.Worksheets("Sheet1") '<- Refer to the sheet you want

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Find the last row of column A

        For i = 2 To LastRow '<- Loop from row 2 to last row

            If IsDate(.Range("A" & i).Value) Then '<- Check if column A row i is date
                'Code Here
            End If

        Next i

    End With

End Sub