So i think i managed to make it work
In the MultiReplace sub I've done some modifications
Set Dict = CreateObject("Scripting.Dictionary")
Set Wks = Workbooks("test.xlsx").Sheets("Sheet2") 'changes workbook to get the find/replace data
and
Set Wks = ActiveWorkbook.Worksheets(1)
for the loop i've used this
Public Function IsFileOpen(strFileName As String) As Boolean
On Error Resume Next 'Ignore any errors (i.e. if workbook is not open)
Set wrkFileName = Workbooks(strFileName)
If wrkFileName Is Nothing Then
IsFileOpen = False
Else
IsFileOpen = True
End If
On Error GoTo 0 'Nullify above error handler
End Function
Sub Macro1()
Dim strDir As String, _
strFileType As String
Dim objFSO As Object, _
objFolder As Object, _
objFile As Object
Dim intCounter As Integer
strDir = ActiveWorkbook.Path & "\folder\" 'Change to suit
strFileType = "xlsx" 'change to suit if required
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDir)
Application.ScreenUpdating = False
For Each objFile In objFolder.Files
'If the file in the 'strDir' directory is not this workbook, then...
If objFile.Name <> ThisWorkbook.Name Then
If objFile.Name Like "*." & strFileType Then
'...check to see if it's open. If it is...
If IsFileOpen(objFile.Name) = True Then
'...run the 'MyMacro' passing the active workbook variable with it and _
increment the counter.
Call MyMacro(objFile.Name)
intCounter = intCounter + 1
'Else, _
1. Open the file, _
2. Run the 'MyMacro' passing the active workbook variable with it, _
3. Save the changes and close the file, and _
4. Increment the counter.
Else
Workbooks.Open (strDir & "\" & objFile.Name), UpdateLinks:=False
Call MyMacro(objFile.Name)
Workbooks(objFile.Name).Close SaveChanges:=True
intCounter = intCounter + 1
End If
End If
End If
'Release memory
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Next objFile
Application.ScreenUpdating = True
Select Case intCounter
Case Is = 0
MsgBox "There were no """ & strFileType & """ file types in the """ & strDir & """ directory for the desired macro to be run on.", vbExclamation, "Data Execution Editor"
Case Is = 1
MsgBox "The desired macro has been run on the only """ & strFileType & """ file in the """ & strDir & """ directory.", vbInformation, "Data Execution Editor"
Case Is > 1
MsgBox "The desired macro has now been run on the " & intCounter & " files in the """ & strDir & """ directory.", vbInformation, "Data Execution Editor"
End Select
End Sub
Sub MyMacro(strDesiredWkb As String)
MultiReplace
MsgBox strDesiredWkb
End Sub
the spreadsheet should be in a folder and the files you want run the macro on should be in a new folder (inside that folder)
and now i'm able to run the find/replace macro on all my files :>
Bookmarks