[英]Run time Error, Subscript out of range
I am trying to run a macro to generate a data and it was working initially however, now it is giving an error, "subscipt out of range". 我正在尝试运行一个宏来生成数据,但是它最初在工作,但是现在出现错误,“超出范围”。 Please I need assistance:
请我需要协助:
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
I would place my bet on one of the sheet names not being accurate. 我将赌注押在一张不准确的表名上。 OR the focus moving to the wrong workbook.
或者将焦点移到错误的工作簿。 And hence you should avoid the use of
.Select/.Activate
INTERESTING READ 因此,您应该避免使用
.Select/.Activate
INTERESTING READ
Your code can be re-written as (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
Also when you are working with events, always use error handling else if you get an error, those events are not restored to defaults. 同样,在处理事件时,请始终使用错误处理,否则,如果发生错误,这些事件将不会恢复为默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.