PressEscape
New Member
- Joined
- May 2, 2024
- Messages
- 22
- Office Version
- 2021
- Platform
- Windows
I have bill of material workbooks in multiple sub folders that I wish to replace cell content when something is revised
e.g "Flow Restrictor 1.0mm orifice" to become "Flow Restrictor 2.0mm orifice"
I've managed to get some code to loop through the top folder shown below.
Can anyone help with the code to get it to loop through subfolders as well please.
e.g "Flow Restrictor 1.0mm orifice" to become "Flow Restrictor 2.0mm orifice"
I've managed to get some code to loop through the top folder shown below.
Can anyone help with the code to get it to loop through subfolders as well please.
VBA Code:
Sub ReplaceInFolder()
Dim strPath As String
Dim strFile As String
Dim wbk As Workbook
Dim wsh As Worksheet
Dim strFind As String
Dim strReplace As String
Dim i As Long
'strFind = InputBox("Enter text to find")
strFind = ("Flow Restrictor 1.0mm orifice")
If strFind = "" Then
MsgBox "No find text found!", vbExclamation
Exit Sub
End If
strReplace = ("Flow Restrictor 2.0mm orifice")
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then
strPath = .SelectedItems(1)
Else
MsgBox "No folder selected!", vbExclamation
Exit Sub
End If
End With
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
Application.ScreenUpdating = False
strFile = Dir(strPath & "*.xlsx*")
Do While strFile <> ""
Set wbk = Workbooks.Open(fileName:=strPath & strFile, AddToMRU:=False)
For Each wsh In wbk.Worksheets
wsh.Cells.Replace What:=strFind, Replacement:=strReplace, _
LookAt:=xlWhole, MatchCase:=False
Next wsh
wbk.Close SaveChanges:=True
strFile = Dir
Loop
MsgBox "File run OK", vbExclamation
Application.ScreenUpdating = True
End Sub