【问题标题】:If Then Statement with datesIf Then 带有日期的语句
【发布时间】:2017-11-29 22:02:18
【问题描述】:

我正在编写一个命令按钮,以便它可以为我提供两个日期之间出售的资产。

运行时错误 13: mismatch 当前在我运行代码时出现。

我更改了If Then 语句,但仍然出现同样的错误。下面是我试图用按钮完成的一个例子。我希望代码查看工作表中的所有日期。然后将两个日期之间的那些行复制然后粘贴到另一个工作表中。

| Asset#    | Asset Name S# | Sold     |
|-----------|---------------|----------|
| 4555#1202 | Scissor Lift  | 12/15/12 |
| 4898#1204 | Light Tower   | 11/12/15 |

这是我为命令按钮运行的代码:

Private Sub CommandButton9_Click() 

    Worksheets("Paste2").Rows("2:1000").Delete

    Dim erow As Long, start As Date, enddt As Date

    x = 2

    '*The 12 represents the column which contains the date for when the asset was sold*

    Do While Worksheets("Asset Info").Cells(x, 12) <> ""

        start = DateValue("October 1,2016")
        enddt = DateValue("October 31,2016")

        If Worksheets("Asset Info").Range("L1:L2500") > start And Worksheets("Asset Info").Range("L1:L2500") < enddt Then  '*This is where the error occurs in the code*        

            Worksheets("Asset Info").Rows(x).Copy
            Worksheets("Paste2").Activate

            erow = Worksheets("Paste2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row        
            ActiveSheet.Paste Destination:=Worksheets("Paste2").Rows(erow)    
        End If

        Worksheets("Paste2").Activate       
        x = x + 1    

    Loop    

    Worksheets("Paste2").Activate
End Sub

任何建议将不胜感激。

an example of sheet im trying to copy

命令按钮的其他形式

`Worksheets("Paste2").Rows("2:1000").Delete


Dim erow As Long, LastRow As Long, start As Date, enddt As Date

LastRow = Workheets("Asset Info").Cells(Workheets("Asset Info").Rows.Count, "L").End(xlUp).Row

For x = 2 To LastRow

    start = DateValue("October 1,2016")
    enddt = DateValue("October 31,2016")

    If Worksheets("Asset Info").Cells(x, 12).Value <> "" > start And Worksheets("Asset Info").Cells(x, 12).Value <> "" < enddt Then


        Worksheets("Asset Info").Rows(x).Copy
        Worksheets("Paste2").Activate

        erow = Worksheets("Paste2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

        ActiveSheet.Paste Destination:=Worksheets("Paste2").Rows(erow)

   End If
Next x

End Sub`

【问题讨论】:

  • 我有一个建议:插入一个 Excel 表格,然后您就可以轻松访问其自动过滤器,根据需要过滤列,然后将表格主体的可见单元格复制到新工作表中。
  • 实际上是否有一个名为“资产信息”以及“资产信息”的工作表?这将是一个不同的错误代码。请在代码顶部使用 Option Explicit。

标签: excel vba date


【解决方案1】:

我现在已经尝试并测试了以下代码,它应该可以满足您的期望:

Sheets("Paste2").Rows("2:1000").Delete

Dim erow As Long, start As Date, enddt As Date

LastRow = Sheets("Asset Info").Cells(Sheets("Asset Info").Rows.Count, "L").End(xlUp).Row

For x = 2 To LastRow
    start = DateValue("October 1,2016")
    enddt = DateValue("October 31,2016")

    If Sheets("Asset Info").Cells(x, 12).Value > start And Sheets("Asset Info").Cells(x, 12).Value < enddt Then
        Sheets("Asset Info").Rows(x).Copy
        erow = Sheets("Paste2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Sheets("Paste2").Paste Destination:=Sheets("Paste2").Rows(erow)
   End If
Next x

【讨论】:

  • 该更改适用于一些资产,但我收到了一个错误代码
  • Do While Worksheets("Asset Info").Cells(x, 12) "" 这部分代码告诉我一个不匹配错误run time error 13
  • 更新了我的答案以反映您的 cmets。
  • 我按照建议更改了代码的一部分,但命令甚至没有运行。我将新代码放在原始帖子中。 @Xabier
  • 抱歉,我错过了 IF 语句中的单元格引用,请查看我的更新答案,..
猜你喜欢
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
相关资源
最近更新 更多