【发布时间】: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。