Attached is the GetOpenFileName and Loop as well as an example of a Sub I might Call within it.
My main question is: Is there a different way that I should be doing this that would be less volatile? I was hoping to be able to get it working well enough that whenever I need to do some operation on several workbooks I could just call that procedure from this loop and it work. I have used it with a couple different processes and it works fine but others it gives me trouble. Specifically I have code in ThisWorkbook of many of the files I need to manipulate as below:
Private Sub Workbook_Open() 'Should be saved in the desired workbook's ThisWorkbook Object
Dim Wksheet As Worksheet
'Step 1: Protect the sheet with a password
For Each Wksheet In Sheets
If Wksheet.ProtectScenarios Then
Wksheet.Unprotect Password:="locked"
Wksheet.Protect Password:="locked", UserInterfaceOnly:=True
Else
Wksheet.Protect Password:="locked", UserInterfaceOnly:=True
End If
Next Wksheet
End Sub
My understanding is that this would allow me to manipulate with a macro because it would always have UserInterfaceOnly:=True once opened.
This does not work for me. Why? I have to unlock the sheets within the sub to get it to work reliably.
One more note is that I usually run this macro from any random workbook. I am trying hard to learn VBA but I can't seem grasp it like I would hope.
Sub ModCondForm() 'Modifies the formula of the conditional formatting of two cells in the Shift Reports
Range("B20").FormatConditions(1).Modify _
Type:=xlExpression, Formula1:="=OR(F20<0.0015%, F20>0.0018%)"
Range("B21").FormatConditions(1).Modify _
Type:=xlExpression, Formula1:="=OR(F21<0.15%, F21>0.17%)"
End Sub
Sub LoopThroughSelFiles2()
Dim FolderPath As String
Dim SelectedFiles As Variant
Dim NFile As Long
Dim FileName As String
Dim WorkBk As Workbook
' Turns screen updating off to speed up macro execution.
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
' Modify this folder path to point to the files you want to use.
FolderPath = "C:\"
' Set the current directory to the folder path.
ChDrive FolderPath
ChDir FolderPath
' Open the file dialog box and filter on Excel files, allowing multiple files
' to be selected.
SelectedFiles = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
' Handles the error if user clicks the cancel button or x's out of the "Open File" dialogue box.
' On Error GoTo ErrorHandler
' Loop through the list of returned file names
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
' Set FileName to be the current workbook file name to open.
FileName = SelectedFiles(NFile)
' Open the current workbook.
Set WorkBk = Workbooks.Open(FileName)
Call ModCondForm
' Close the workbook saving changes made and move on to the next file.
WorkBk.Close savechanges:=True
Next NFile
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Exit Sub
'ErrorHandler:
'' Select Case Err
'' Case 13
'' Exit Sub
' Case 4004
' Resume Next
'' Case Else
'' Resume Next
' End Select
End Sub
Bookmarks