两年前的宏出现突然的运行时错误“9”
Sudden Runtime error '9' on a two year old macro
我的一个宏出现了问题,我已经使用了几个月,但问题不大。该宏旨在重新格式化 excel 报告并将其插入到 excel 内的不同工作簿中。今天,我不断遇到这个消息:
runtime error '9': subscript out of range
当我select调试时,它突出显示了这行代码:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
我不是程序员。我已经使用宏按钮来复制我为缩短任务而做的事情,但是除了将错误和突出显示的代码复制并粘贴到搜索引擎中以查看其他人可能尝试过的内容之外,我还不够聪明,无法解决逻辑问题。该代码一直有效,我没有做任何不同的事情,但今天它抛出“9”错误。我尝试过重命名工作表以匹配代码,所以基本上 "Sheet1"。我复制了上个月的工作表,删除了旧数据,并尝试了 运行 宏。我什至按照 google 的建议调整了代码,发现一个人遇到了类似的问题,但我刚刚创建了一个“1004”错误,因为我不完全理解 xlTop 与 xlDown 的逻辑,除了隐含的方向。那没有用,所以我回到原点。
这是我的宏代码。很简单。
Sub UserStats()
'
' UserStats Macro
'
Application.ScreenUpdating = False 'Doesn't show the macro
run on the screen, speeds up program
'
Cells.Select
With Selection
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("B:C").Select
Selection.Delete Shift:=xlToLeft
Columns("C:D").Select
Selection.Delete Shift:=xlToLeft
Columns("D:I").Select
Selection.Delete Shift:=xlToLeft
Rows("1:7").Select
Selection.Delete Shift:=xlUp
Columns("A:D").Select
' SortUserStats Macro
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A:D")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' CopyUserStats Macro
Cells.Select
Selection.RowHeight = 12
Range("A2:D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks("User Stats Prep.xlsx").Worksheets(1).Activate
Cells(Range("A1").End(xlDown).Row + 1, 1).Select
Selection.Insert Shift:=xlDown
Cells.Select
Selection.RowHeight = 12
Cells(Range("A1").End(xlDown).Row + 1, 1).Select
Application.CutCopyMode = False
ActiveWorkbook.Close SaveChanges:=True
ActiveWorkbook.Close SaveChanges:=False
End Sub
感谢任何建议,否则我将逐行复制和粘贴样式添加新数据。
我已经尝试重写它,这样它就不会像宏记录器通常创建的那样多余。如果这不起作用或者它的工作方式与以前不同,请准确描述什么是 wrong/the 错误
Sub UserStats()
'
' UserStats Macro
'
Application.ScreenUpdating = False 'hides screen, speeds up program
With ActiveWorkbook.Sheets(1)
'format all sheet1 cells
With .Cells
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.RowHeight = 12
End With
'delete A:D, D:I, and 1:7
.Columns("A:D").Delete Shift:=xlToLeft
.Columns("D:I").Delete Shift:=xlToLeft
.Rows("1:7").Delete Shift:=xlUp
'Sort UserStats
With .Columns("A:D").Sort
.SortFields.Clear
.SortFields.Add _
key:=Range("A1"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
.SetRange Range("A:D")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Copy UserStats
ActiveWorkbook.Sheets(1).Range(Range("A2:D2"), Range("A2:D2").End(xlDown)).copy
End With
With Workbooks("User Stats Prep.xlsx").Worksheets(1)
.Cells.RowHeight = 12
.Cells(Range("A1").End(xlDown).row + 1, 1).Insert Shift:=xlDown
End With
Workbooks("User Stats Prep.xlsx").Close SaveChanges:=True
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub
我的一个宏出现了问题,我已经使用了几个月,但问题不大。该宏旨在重新格式化 excel 报告并将其插入到 excel 内的不同工作簿中。今天,我不断遇到这个消息:
runtime error '9': subscript out of range
当我select调试时,它突出显示了这行代码:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
我不是程序员。我已经使用宏按钮来复制我为缩短任务而做的事情,但是除了将错误和突出显示的代码复制并粘贴到搜索引擎中以查看其他人可能尝试过的内容之外,我还不够聪明,无法解决逻辑问题。该代码一直有效,我没有做任何不同的事情,但今天它抛出“9”错误。我尝试过重命名工作表以匹配代码,所以基本上 "Sheet1"。我复制了上个月的工作表,删除了旧数据,并尝试了 运行 宏。我什至按照 google 的建议调整了代码,发现一个人遇到了类似的问题,但我刚刚创建了一个“1004”错误,因为我不完全理解 xlTop 与 xlDown 的逻辑,除了隐含的方向。那没有用,所以我回到原点。
这是我的宏代码。很简单。
Sub UserStats()
'
' UserStats Macro
'
Application.ScreenUpdating = False 'Doesn't show the macro
run on the screen, speeds up program
'
Cells.Select
With Selection
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("B:C").Select
Selection.Delete Shift:=xlToLeft
Columns("C:D").Select
Selection.Delete Shift:=xlToLeft
Columns("D:I").Select
Selection.Delete Shift:=xlToLeft
Rows("1:7").Select
Selection.Delete Shift:=xlUp
Columns("A:D").Select
' SortUserStats Macro
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A:D")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' CopyUserStats Macro
Cells.Select
Selection.RowHeight = 12
Range("A2:D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks("User Stats Prep.xlsx").Worksheets(1).Activate
Cells(Range("A1").End(xlDown).Row + 1, 1).Select
Selection.Insert Shift:=xlDown
Cells.Select
Selection.RowHeight = 12
Cells(Range("A1").End(xlDown).Row + 1, 1).Select
Application.CutCopyMode = False
ActiveWorkbook.Close SaveChanges:=True
ActiveWorkbook.Close SaveChanges:=False
End Sub
感谢任何建议,否则我将逐行复制和粘贴样式添加新数据。
我已经尝试重写它,这样它就不会像宏记录器通常创建的那样多余。如果这不起作用或者它的工作方式与以前不同,请准确描述什么是 wrong/the 错误
Sub UserStats()
'
' UserStats Macro
'
Application.ScreenUpdating = False 'hides screen, speeds up program
With ActiveWorkbook.Sheets(1)
'format all sheet1 cells
With .Cells
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.RowHeight = 12
End With
'delete A:D, D:I, and 1:7
.Columns("A:D").Delete Shift:=xlToLeft
.Columns("D:I").Delete Shift:=xlToLeft
.Rows("1:7").Delete Shift:=xlUp
'Sort UserStats
With .Columns("A:D").Sort
.SortFields.Clear
.SortFields.Add _
key:=Range("A1"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
.SetRange Range("A:D")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Copy UserStats
ActiveWorkbook.Sheets(1).Range(Range("A2:D2"), Range("A2:D2").End(xlDown)).copy
End With
With Workbooks("User Stats Prep.xlsx").Worksheets(1)
.Cells.RowHeight = 12
.Cells(Range("A1").End(xlDown).row + 1, 1).Insert Shift:=xlDown
End With
Workbooks("User Stats Prep.xlsx").Close SaveChanges:=True
With Application
.CutCopyMode = False
.ScreenUpdating = True
End With
End Sub