【问题标题】:VBA Code runs in excel 2013 but not in 2010VBA 代码在 excel 2013 中运行,但在 2010 中不运行
【发布时间】:2014-01-14 21:58:41
【问题描述】:

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

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

说:类型不匹配,数字是:运行时错误'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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    EntryDate As Date 更改为EntryDate As Variant

    还有换行,

    EntryDate = CDate(InputBox("Insert Date", "Insert Date", vbOKCancel))` to
    

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

    在那一行之后

    添加这一行

    If EntryDate = False Then Exit Sub
    

    在这行之后,你可以写

    EntryDate  = Cdate(EntryDate)
    

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

    来自 cmets 的跟进

    这是你正在尝试的吗?

    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
    

    【讨论】:

    • ok 我应该如何处理 else 语句?如果没有 and if 语句,它将是单独的吗?
    • 一会儿更新我的帖子
    • +1 快速回答!我什至没有时间阅读这个问题:)
    • @SiddharthRout 是的,这就是我想要做的,这里是 Cdate,因为我希望应用程序将日期值作为日期格式插入,以便支持过滤
    • @MohammadTaha: HERE 是我的口头禅:p
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多