我正在尝试运行一个宏来生成数据并且它最初工作,但现在它给出了一个错误,“subscipt out of range”。我需要帮助:
Sub paste()
'
' paste Macro
'
Application.ScreenUpdating = False
Application.Calculation = xlManual
MyPath = ThisWorkbook.Path
Sheets("Raw_Data_Agent").Visible = True
Sheets("Raw_Data_Agent").Select
Columns("B:P").Select
Selection.ClearContents
Workbooks.Open Filename:=MyPath & "\Raw_Data_Agent.xls"
Columns("A:O").Select
Selection.Copy
Workbooks("Real Time Agent AHT And Login Tracker").Activate
Sheets("Raw_Data_Agent").Select
Range("B1").Select
ActiveSheet.paste
Sheets("Raw_Data_Agent").Visible = False
Workbooks("Raw_Data_Agent").Activate
Application.DisplayAlerts = False
ActiveWorkbook.Close False
Sheets("AM And Process Wise").Select
Calculate
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
我会把我的赌注押在其中一张不准确的表格上。或者焦点移动到错误的工作簿。因此,您应该避免使用.Select/.Activate
INTERESTING READ
您的代码可以重写为(UNTESTED)
Sub paste()
Dim thisWb As Workbook, thatWb As Workbook, AnotherWb as Workbook
Dim thisWs As Worksheet
Dim nCalc
On Error GoTo Whoa
With Application
.ScreenUpdating = False
nCalc = .Calculation
.Calculation = xlManual
End With
Set thisWb = ThisWorkbook
Set thisWs = thisWb.Sheets("Raw_Data_Agent")
Set AnotherWb = Workbooks("Real Time Agent AHT And Login Tracker")
MyPath = thisWb.Path
With thisWs
.Visible = True
.Columns("B:P").ClearContents
Set thatWb = Workbooks.Open(Filename:=MyPath & "\Raw_Data_Agent.xls")
thatWb.Columns("A:O").Copy AnotherWb.Sheets("Raw_Data_Agent").Range("B1")
.Visible = False
Application.DisplayAlerts = False
thatWb.Close False
End With
thisWb.Sheets("AM And Process Wise").Calculate
thisWb.RefreshAll
LetsContinue:
With Application
.ScreenUpdating = True
.Calculation = nCalc
.DisplayAlerts = True
End With
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
此外,当您处理事件时,如果出现错误,请始终使用错误处理,否则这些事件不会恢复为默认值。