我一直在使用一个宏(带有activex按钮)大约5个小时。我需要它能够在添加时根据设施自动对新数据进行排序,然后将其放入枢轴并打开包含不同设施数据的新选项卡。我已经遇到了本书中的每个编译错误。我已经遵循了一些建议,现在我遇到了一个错误:Run-Time error '9', Subscript out of range
。我是初学者,所以请原谅我,如果这不是一个聪明的问题。我要发布代码。
'
' CreateWorkList Macro
' Create a work list by the facility
'
' Keyboard Shortcut: Ctrl+Shift+W
'
Cells.Select
ActiveSheet.Range("$C$1:$W$2897").AutoFilter Field:=2, Criteria1:="2"
Selection.Copy
ActiveSheet.Range("$C$1:$W$2897").AutoFilter Field:=2
Range("C140").Select
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 13
ActiveWindow.ScrollColumn = 16
ActiveWindow.ScrollColumn = 17
ActiveWindow.ScrollColumn = 19
ActiveWindow.ScrollColumn = 21
ActiveWindow.ScrollColumn = 24
ActiveWindow.ScrollColumn = 25
ActiveWindow.ScrollColumn = 26
ActiveWindow.ScrollColumn = 27
ActiveWindow.ScrollColumn = 28
ActiveWindow.ScrollColumn = 30
ActiveWindow.ScrollColumn = 31
ActiveWindow.ScrollColumn = 32
ActiveWindow.ScrollColumn = 36
ActiveWindow.ScrollColumn = 37
ActiveWindow.ScrollColumn = 40
ActiveWindow.ScrollColumn = 42
ActiveWindow.ScrollColumn = 44
ActiveWindow.ScrollColumn = 45
ActiveWindow.ScrollColumn = 46
ActiveWindow.ScrollColumn = 47
ActiveWindow.ScrollColumn = 54
ActiveWindow.ScrollColumn = 55
ActiveWindow.ScrollColumn = 57
ActiveWindow.ScrollColumn = 60
ActiveWindow.ScrollColumn = 63
ActiveWindow.ScrollColumn = 65
ActiveWindow.ScrollColumn = 69
ActiveWindow.ScrollColumn = 71
ActiveWindow.ScrollColumn = 77
ActiveWindow.ScrollColumn = 79
ActiveWindow.ScrollColumn = 63
ActiveWindow.ScrollColumn = 1
Range("C1:W1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R135C21", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Sheet1").Select
Cells(1, 26).Select
ActiveWindow.SmallScroll ToRight:=5
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Campus")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("OPID")
.Orientation = xlRowField
.Position = 1
End With
ActiveWindow.SmallScroll ToRight:=6
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Campus")
.PivotItems("Celebration").Visible = False
.PivotItems("East Orlando").Visible = False
.PivotItems("Kissimmee").Visible = False
.PivotItems("Orlando").Visible = False
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("AcctNum"), "Sum of AcctNum", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of AcctNum")
.Caption = "Counts of AcctNum"
.Function = xlCount
End With
Range("AA10").Select
Selection.ShowDetail = True
下一行是我收到错误的地方:
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Altamonte"
Sheets("Sheet1").Select
Range("AB10").Select
Selection.ShowDetail = True
Sheets("Sheet3").Select
Sheets("Sheet3").Name = "Apopka"
Sheets("Sheet1").Select
Range("AC10").Select
Selection.ShowDetail = True
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Sheet1"
Sheets("Sheet4").Select
Sheets("Sheet4").Name = "Winter Park"
Range("C21").Select