2014-02-10 71 views
0

我想運行一個宏來生成一個數據,但它最初工作,但現在它給出了一個錯誤,「subscipt超出範圍」。我需要幫助:運行時錯誤,下標超出範圍

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

此腳本在哪一行中斷? – Taosique

+0

工作簿(「Real_Time_Agent_AHT_And_Login_Tracker」)。激活 – user3275237

+0

工作簿(「Real_Time_Agent_AHT_And_Login_Tracker」)。激活,此處突破 – user3275237

回答

0

我會把我的賭注放在一個表名不準確。或者將焦點轉移到錯誤的工作簿上。因此,你應該避免使用.Select/.ActivateINTERESTING READ

您的代碼可以被重新寫爲(未經測試)

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 

此外,當您正在處理事件,總是用錯誤處理否則,如果你得到一個錯誤,那些事件不會恢復到默認值。

+0

感謝您的評論,但是,當我嘗試逐行運行代碼時,這正是問題所在:工作簿(「Real_Time_Agent_AHT_And_Login_Tracker」)。激活 – user3275237

+0

您是否有通過該名稱打開的工作簿? –

+0

我剛修改了代碼,如果你在'Set AnotherWb = Workbooks(「實時代理AHT和登錄追蹤器」)行出現錯誤''那麼這意味着它找不到具有該名稱的工作簿。 –

相關問題