需要VBA来识别日期而不仅仅是数字

时间:2014-02-02 21:01:06

标签: excel excel-vba vba

在我的代码中,应该检查列a中是否存在日期,如果是,则询问是否应该替换该条目。但我不能让它读取日期,数字很好。代码是:

Dim Answer As Integer
  Dim iRow As Long
  Dim Msg As String
  Dim PartCell As Range
  Dim Rng As Range
  Dim RngEnd As Range
  Dim ws As Worksheet

    Set ws = Worksheets("OP")

   'get all the database entires
    Set Rng = ws.Range("A2")
    Set RngEnd = ws.Cells(Rows.Count, "A").End(xlUp)
    Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, ws.Range(Rng, RngEnd))


   'Check if Date for Entry exits in database
    Set PartCell = Rng.Find(OPdate, , xlValues, xlWhole, xlByRows, xlPrevious, False)
      If Not PartCell Is Nothing Then
        Msg = "Entry for '" & OPdate & "' has already been entered." & vbCrLf _
            & "Do wish to overwrite this record?"
        Answer = MsgBox(Msg, vbQuestion + vbYesNo)
        If Answer = vbNo Then Exit Sub
        iRow = PartCell.Row
      Else
        iRow = RngEnd.Row + 1
      End If

   'copy the data to the database
    ws.Cells(iRow, 1).Value = Me.OPdate.Value
    ws.Cells(iRow, 2).Value = Me.OPst.Value
    ws.Cells(iRow, 3).Value = Me.OPia.Value

1 个答案:

答案 0 :(得分:0)

从我所看到的,你没有在任何地方定义你的日期变量OPdate,即你需要添加如下内容:

Dim OPdate As Date
OPdate = Date         'Date returns current date