Activate Workbook in Loop through folder

gittymoe

Board Regular
Joined
Apr 23, 2005
Messages
79
Guys, I am trying to run the below to loop through each workbook in a designated folder and remove the protected status and all sheets with the exception of the first worksheet. When I run the highlighted code by itself on an active workbook it works but the way I have it set up to loop does not seem to activate the workbook to run the highlighted code.


Sub RunOnAllFilesInFolder()
Dim folderName As String, eApp As Excel.Application, FileName As String
Dim wb As Workbook, ws As Worksheet, currWs As Worksheet, currWb As Workbook, currwb1 As Workbook
Dim fDialog As Object: Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
Set currWb = ActiveWorkbook: Set currWs = ActiveSheet: Set currwb1 = ActiveWorkbook

'Create a separate Excel process that is invisible
Set eApp = New Excel.Application: eApp.Visible = False

'Search for all files in folder [replace *.* with your pattern e.g. *.xlsx]
folderName = "MyFolderName"
FileName = Dir(folderName & "\*.xls")
Do While FileName <> ""
'Update status bar to indicate progress
Application.StatusBar = "Processing " & folderName & "\" & FileName

Set wb = eApp.Workbooks.Open(folderName & "\" & FileName)
'currWb.Unprotect "JJ"

'YOUR CODE HERE
Dim sht As Object
Application.DisplayAlerts = False
For Each sht In ActiveWorkbook.Sheets
If sht.Index <> 1 Then
sht.Delete
End If

Next
'...
wb.Close SaveChanges:=True 'Close opened worbook w/o saving, change as needed
Debug.Print "Processed " & folderName & "\" & FileName
FileName = Dir()
Loop
eApp.Quit
Set eApp = Nothing
'Clear statusbar and notify of macro completion
Application.StatusBar = ""
MsgBox "Completed executing macro on all workbooks"
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Change ActiveWorkbook to wb. That is the workbook reference previously established.

For Each sht In wb.Sheets
 
Upvote 0

Forum statistics

Threads
1,223,764
Messages
6,174,364
Members
452,558
Latest member
jswan83

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top