2014-09-11 28 views
2

我是VBA的新手。我寫了一段代碼來刪除特定的表單。執行該刪除表宏後,excel宏停止執行。它沒有執行futher ..Excel Vba在刪除表格後停止執行

這裏是我的代碼..

Sub CopyAcross() 
Dim sheetName As String 
sheetName = "Master_Base" 
If WorksheetExists(sheetName) Then 
DeleteSheet (sheetName) 
End If 
MsgBox "Debug" 

Workbooks("Master_Base.csv").Sheets("Master_Base").Copy Before:=Workbooks("Copy of test.xlsm").Worksheets("Sheet3") 
End Sub 

Sub DeleteSheet(strSheetName As String) 
' deletes a sheet named strSheetName in the active workbook 
Application.DisplayAlerts = False 
Sheets(strSheetName).Delete 
Application.DisplayAlerts = True 
End Sub 

任何一個可以幫助此,

在此先感謝。

+0

作品對我蠻好 – 2014-09-11 06:23:39

+0

我已經通過線通過copyAcross API。它設置斷點執行的行在表格(strSheetName).Delete處完全停止。 – kayle 2014-09-11 06:27:36

+0

移除'Application.DisplayAlerts = False',看看你收到了什麼消息,如果有的話? – 2014-09-11 06:28:32

回答

2

由於您正在使用多個工作簿,請使用對象。否則,你的代碼MAY與錯誤的工作簿/工作表

試試這個(UNTESTED

Sub CopyAcross() 
    Dim wbI As Workbook, wbO As Workbook 

    '~~> The workbook from where the code is running 
    Set wbO = ThisWorkbook 

    '~~> Here you open the csv 
    Set wbI = Workbooks.Open("E:\OPM\OPM Sheet\Master_Base.csv") 

    '~~> This will delete the sheet if it exists 
    '~~> no need to check if it exists 
    On Error Resume Next 
    Application.DisplayAlerts = False 
    wbO.Sheets("Master_Base").Delete 
    Application.DisplayAlerts = True 
    On Error GoTo 0 

    '~~> The csv will always have 1 sheet 
    '~~> so no need providing a name 
    wbI.Sheets(1).Copy Before:=wbO.Worksheets("Sheet3") 
End Sub 
+0

當我從不同的工作簿執行時代碼完美無缺。執行沒有中斷。謝謝siddharth。 :) – kayle 2014-09-11 09:18:02