我正在使用此代码从已关闭的文件中导入工作表。导入完成后,我想从该工作表中删除一列并切换回主工作表。我尝试使用Columns(4)。删除和EntireColumns.Delete,但没有错误,Excel不采取任何操作。关于更好的方法的任何想法,我可以做到这一点?
'THIS CODE HANDLES THE IMPORT OF THE DATA
Sub ImportFile()
'DEFINITIONS
Dim fileImport As String
Dim srcBook As Workbook
' TURNS OFF SCREEN UPDATING
Application.ScreenUpdating = False
Application.EnableEvents = False
' THE FILE PATH
fileImport = "PATH"
'OPENS THE BOOK
Set sourceB = Application.Workbooks.Open(fileImport)
'COPYS THE SHEET
sourceB.Sheets("Miscellaneous Holds").Copy After:=ThisWorkbook.Sheets (ThisWorkbook.Sheets.Count)
' CLOSES THE BOOK
srcBook.Close True
'ENABLES SCREEN EDIT
Application.ScreenUpdating = True
Application.EnableEvents = True
Sheets("Main").Activate
End Sub
答案 0 :(得分:1)
如果您未在Columns(4).EntireColumn.Delete
声明...
所以请改用sourceB.Sheets("Miscellaneous Holds").Columns(4).EntireColumn.Delete
....
如果您不希望将这些更改保存在源工作簿中,请将saveChanges参数更改为false。
'THIS CODE HANDLES THE IMPORT OF THE DATA
Sub ImportFile()
'DEFINITIONS
Dim fileImport As String
Dim srcBook As Workbook
Dim srcSheet As Worksheet
' TURNS OFF SCREEN UPDATING
Application.ScreenUpdating = False
Application.EnableEvents = False
' THE FILE PATH
fileImport = "PATH"
'OPENS THE BOOK
Set srcBook = Application.Workbooks.Open(fileImport)
Set srcSheet = sourceB.Sheets("Miscellaneous Holds")
srcSheet.Columns(4).EntireColumn.Delete
'COPYS THE SHEET
srcSheet.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
' CLOSES THE BOOK
srcBook.Close False
'ENABLES SCREEN EDIT
Application.ScreenUpdating = True
Application.EnableEvents = True
ThisWorkbook.Sheets("Main").Activate
End Sub