VBA代码在excel 2013中运行,但在2010年没有

4 excel vba excel-vba

我使用excel 2013编写的以下代码适用于excel 2013,但是当我在我公司的2010版excel上尝试它时,当我单击输入框对话框上的取消按钮并停在代码行时会产生错误:

EntryDate = CDate(InputBox("Insert Date", "Insert Date", vbOKCancel))
Run Code Online (Sandbox Code Playgroud)

说:类型不匹配,数字是:运行时错误'13':

为什么?

    Sub InsertNewEntry()
'
' InsertNewEntry Macro
' To Insert New Entry for exchange rates
'

Dim LastRow As Integer, EntryDate As Date
Dim EURtoUSD As Double, JODtoUSD As Double, ILStoUSD As Double

' determine the number of the last row entered
LastRow = ThisWorkbook.Worksheets("Exchange Rates Template").Cells(Rows.Count, 2).End(xlUp).Row

'determine if last date is last day of the year
   If Cells(LastRow, 2) = #12/31/2014# Then
    MsgBox "You are not allowed to insert a date above " & "31/12/ " & Cells(4, 1).Value
    Exit Sub
   Else
    Cells(LastRow, 4).Select
    Selection.ListObject.ListRows.Add AlwaysInsert:=False
    Cells(LastRow, 8).Select
    Selection.ListObject.ListRows.Add AlwaysInsert:=False
    Cells(LastRow, 12).Select
    Selection.ListObject.ListRows.Add AlwaysInsert:=False
    Cells(LastRow + 1, 2).Select
    EntryDate = CDate(InputBox("Insert Date", "Insert Date", vbOKCancel))
      If EntryDate <> "" Then
       Cells(LastRow + 1, 2) = EntryDate
       Cells(LastRow + 1, 3) = "EUR to USD"
       Cells(LastRow + 1, 6) = EntryDate
       Cells(LastRow + 1, 7) = "JOD to USD"
       Cells(LastRow + 1, 10) = EntryDate
       Cells(LastRow + 1, 11) = "ILS to USD"
      Else
       Cells(LastRow + 1, 2).Select
       Selection.ListObject.ListRows(LastRow - 3).Delete
       Cells(LastRow + 1, 6).Select
       Selection.ListObject.ListRows(LastRow - 3).Delete
       Cells(LastRow + 1, 10).Select
       Selection.ListObject.ListRows(LastRow - 3).Delete
       Exit Sub

      End If
   End If

End Sub
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 6

更改EntryDate As Date到EntryDate As Variant

并改变线,

EntryDate = CDate(InputBox("Insert Date", "Insert Date", vbOKCancel))` to
Run Code Online (Sandbox Code Playgroud)

至

EntryDate = InputBox("Insert Date", "Insert Date", vbOKCancel)
Run Code Online (Sandbox Code Playgroud)

然后在那条线之后

添加此行

If EntryDate = False Then Exit Sub
Run Code Online (Sandbox Code Playgroud)

在这一行之后,你可以写

EntryDate  = Cdate(EntryDate)
Run Code Online (Sandbox Code Playgroud)

原因很简单,因为Cdate(False)会给你00:00:00并且Cdate(True)会给你29/12/1899

跟进评论

这是你在尝试什么?

Dim EntryDate As Variant

EntryDate = InputBox("Insert Date", "Insert Date", vbOKCancel)

If EntryDate = False Then Exit Sub

If EntryDate <> "" Then
    EntryDate = CDate(EntryDate)
    Cells(LastRow + 1, 2).Value = EntryDate
    Cells(LastRow + 1, 3).Value = "EUR to USD"
    Cells(LastRow + 1, 6).Value = EntryDate
    Cells(LastRow + 1, 7).Value = "JOD to USD"
    Cells(LastRow + 1, 10).Value = EntryDate
    Cells(LastRow + 1, 11).Value = "ILS to USD"
Else
    Cells(LastRow + 1, 2).ListObject.ListRows(LastRow - 3).Delete
    Cells(LastRow + 1, 6).ListObject.ListRows(LastRow - 3).Delete
    Cells(LastRow + 1, 10).ListObject.ListRows(LastRow - 3).Delete
    Exit Sub
End If
Run Code Online (Sandbox Code Playgroud)

  • +1快速回答!我甚至没有时间阅读这个问题:) (2认同)
  • @MohammadTaha:[HERE](http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select)是我的口头禅:p (2认同)