【发布时间】:2018-02-01 20:36:53
【问题描述】:
我有一个准备报告的 Excel 宏。该宏每天使用,但今天抛出了上述错误。错误发生在给定代码的最后一行。人们有没有遇到过这个问题? 宏从中提取(并获取错误)的第二个表变得非常大,这会是问题的原因吗?
Windows("Worldwide_Backlog.xlsm").Activate 'Save File as Worldwide_Backlog'
Worksheets(3).Activate
ActiveSheet.PivotTables("PivotTable1").PivotFields("SubFamily2"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("SubFamily2").CurrentPage = _
"DAS"
ActiveSheet.PivotTables("PivotTable1").PivotFields("Product Family"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Product Family"). _
CurrentPage = "REMI"
'Clear Filters'
ActiveSheet.PivotTables("PivotTable1").PivotFields("Age").ClearAllFilters
'Copy and Paste Table into new Workbook'
Range("A9").Select
Selection.End(xlToRight).Select
Selection.End(xlToLeft).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
'Delete Grand Total from newly pasted table'
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With ActiveSheet
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If .Value = "Grand Total" Then .EntireRow.Delete
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
Windows("Worldwide_Backlog.xlsm").Activate
Worksheets(3).Activate
ActiveSheet.PivotTables("PivotTable1").PivotFields("SubFamily2"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("SubFamily2").CurrentPage = _
"Ra"
Range("A11").Select
Application.CutCopyMode = False
ActiveSheet.PivotTables("PivotTable1").PivotSelect "Order Number", xlButton, _
True
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks.Add
ActiveSheet.Paste
'Delete Grand Total'
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
With ActiveSheet
.Select
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
.DisplayPageBreaks = False
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With .Cells(Lrow, "A")
If Not IsError(.Value) Then
If .Value = "Grand Total" Then .EntireRow.Delete
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
Range("M1").Select
ActiveCell.FormulaR1C1 = "Vlookup"
Range("M2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(C[-12],[Book1]Sheet1!C1,1,0)"
Range("L2").Select 'AutoFills'
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Columns("M:M").Select
Selection.AutoFilter
ActiveSheet.Range("$M$1:$M$21").AutoFilter Field:=1, Criteria1:="=#N/A", _
Operator:=xlOr, Criteria2:="="
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("Book1").Activate
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1).Select 'Uses first blank cell'
ActiveSheet.Paste
【问题讨论】:
-
确保粘贴前的选择实际上选择了一个单元格。在此选择之后设置断点,然后检查工作表;选择将可见。