Excel Vba在删除表单后停止执行
我是VBA新手。 我写了一个代码来删除一个特定的表。 执行该删除工作表macros后,Excelmacros停止执行。 它没有执行进一步..
这是我的代码
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
任何人都可以帮助这个,
提前致谢。
由于您正在使用多个工作簿,请使用对象。 否则,您的代码可能与错误的工作簿/工作表一起工作
试试这个( 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