6. Delete the last worksheet in multiple workbooks within a single folder
Continue tutorials from the previous web page. In this part of the tutorial, the fourth in the order I will show how we can find, remove the last worksheet in each workbook that is located in the same folder.
In our case (if you follow the tutorial from the beginning) I have added temporary extra worksheet 'Sheet5' which I used to create multiple workbooks from the list that was in column 'A'. This work sheet is no longer needed. We will now delete specific Sheet from multiple Workbooks it using the VBA macro below.
'published by www.ic-ims.com
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
'In Case of Cancel
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xlsm"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'--------------- Your action -------------
'Save and Close Workbook
'Get next file name
myFile = Dir
'Message Box when tasks are completed
MsgBox "Task Complete!"
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.ScreenUpdating = True
On Error GoTo err
Application.DisplayAlerts = False 'Disable excel alerts sent while deleting a sheet
'MsgBox ("Last Sheet is successfully deleted")
Application.DisplayAlerts = True 'Enable excel alerts
After starting VBA macros, select a folder that contains multiple workbooks. Wait a certain time. I applied this VBA macro to 100 Excel files and the duration of Excel's work was about 2 minutes.
The next step will be to delete a specific 'Module1' in multiple workbooks in the same folder. If you've been following the whole tutorial from the beginning of this 'Module1' I set up a VBA macro that duplicated 'template.xlsm' 100 times.
List all topics related to complete tutorials
- Creating Top Folders and Sub Folders in One Step
- How to copy a workbook 100 times and create multiple workbooks from the list in the 'A' column
- Rename the first in the order of the worksheet, in multiple workbooks in the same folder
- How to remove the last Worksheet in multiple Workbooks in the same folder
- How to remove a particular VBA module in multiple workbooks in the same folder
- How to save all workbooks as values without formulas in all sub-folders
Click to continue tutorials: ------ >>>>>> The next part of the tutorial Item 5 from list above