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
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