Realtreegirl75
New Member
- Joined
- Aug 28, 2022
- Messages
- 40
- Office Version
- 365
- Platform
- Windows
I have done all the digging I can online to try to find out how to make this work but no matter what I do, this loop never stops! I end up having to ESC out of the sub. The goal is to loop through a folder (the same folder, so I could set a file path pretty easily, if that would help!) and then:
open the first file
unhide a sheet
save changes
close the workbook
move on to the next
But then exit the sub when its done so its not an endless loop!
Can anyone help with this?? I have a dozen ways I could really use this code but at the moment its cause more trouble than its worth!
open the first file
unhide a sheet
save changes
close the workbook
move on to the next
But then exit the sub when its done so its not an endless loop!
Can anyone help with this?? I have a dozen ways I could really use this code but at the moment its cause more trouble than its worth!
Excel Formula:
Sub LoopDIRWorkbooks()
Dim MyFolder As String
Dim MyFile As String
Dim wbk As Workbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
msgbox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = DIR(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> ""
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(fileName:=MyFolder & MyFile)
ActiveWorkbook.Unprotect
ActiveSheet.Unprotect
For Each Sheet In ActiveWorkbook.Worksheets
If Sheet.Name = "Dashboard" Then
Sheet.Visible = True
End If
Next Sheet
ActiveSheet.Protect
ActiveWorkbook.Protect
wbk.Close savechanges:=True
MyFile = DIR 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub