如果那么声明与日期

时间:2017-11-29 22:02:19

标签: vba excel-vba date complete excel

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

运行代码时出现运行时错误13:不匹配

我更改了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`

1 个答案:

答案 0 :(得分:0)

我现在已经尝试并测试了以下代码,它应该按照您的预期执行:

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