【发布时间】:2014-08-21 20:06:21
【问题描述】:
我有一个 Excel 工作簿,它从另外两个工作簿中提取数据。 由于数据每小时更改一次,因此对于同一数据,该宏可能每天使用多次。 所以我只想选择这个日期期间的所有以前的数据并想删除它们。 稍后无论如何都会复制数据。 但是只要我想使用
WBSH.Range(Cells(j, "A"), Cells(lastRow - 1, "M")).Select
代码因错误 1004 应用程序定义或对象定义错误而停止。
仅跟随相关部分的代码的 sn-p。 这里有什么问题?
'Set source workbook
Dim currentWb As Workbook
Set currentWb = ThisWorkbook
Set WBSH = currentWb.Sheets("Tracking")
'Query which data from the tracking files shoud get pulled out to the file
CheckDate = Application.InputBox(("From which date you want to get data?" & vbCrLf & "Format: yyyy/mm/dd "), "Tracking data", Format(Date - 1, "yyyy/mm/dd"))
'states the last entry which is done ; know where to start ; currentWb File
With currentWb.Sheets("Tracking")
lastRow = .Range("D" & .Rows.Count).End(xlUp).Row
lastRow = lastRow + 1
End With
'just last 250 entries get checked since not so many entries are made in one week
j = lastRow - 250
'Check if there is already data to the look up date in the analyses sheet and if so deletes these records
Do
j = j + 1
'Exit Sub if there is no data to compare to prevent overflow
If WBSH.Cells(j + 1, "C").Value = "" Then
Exit Do
End If
Loop While WBSH.Cells(j, "C").Value < CheckDate
If j <> lastRow - 1 Then
'WBSH.Range(Cells(j, "A"), Cells(lastRow - 1, "M")).Select
'Selection.ClearContents
End If
谢谢!
【问题讨论】: