code runs, however does not find/replace values.
I believe issues are in the for loop with worksheets and replacement method
Sub UGA()
'PURPOSE: Loop through all Excel files in a user specified folder and perform a set task on them
Dim wb As Workbook
Dim ws As Worksheet
Dim myPath, myFile, myExtension As String
Dim fnd, rep As Variant
Dim FldrPicker As FileDialog
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'find and replace with blank
fnd = "find this"
rep = ""
'Loop through each worksheet in ActiveWorkbook
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Replace What:=fnd, Replacement:=rep, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next ws
'Save and Close Workbook
wb.Close SaveChanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Complete!"
End Sub
expected to find defined value and replace with blanks ("")
Your code is relying on the Active Workbook
which is the likely culprit.
Change For Each ws In ActiveWorkbook.Worksheets
to For Each ws in wb.Worksheets
to explicitly refer to the workbook.
You have also dimmed you strings fnd
& rep
as variants. These should be strings and the proper way to shorthand declare two variables is Dim fnd as String, rep as String
. The way you went about is actually declaring both variables as variant.
Sub UGA()
'PURPOSE: Loop through all Excel files in a user specified folder and perform a set task on them
Const fnd As String = "find this"
Const rep As String = ""
Const cStrExtensions As String = "*.xls*"
Dim ws As Worksheet
Dim strFolderPath As String ' Search Folder
Dim strFileName As String ' Current File Name (Workbook)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
On Error GoTo ProcedureExit
' Choose Search Folder
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = False Then Exit Sub
strFolderPath = .SelectedItems(1) & "\"
End With
' Loop through folder to determine Current File Name (Workbook).
strFileName = Dir(strFolderPath & cStrExtensions)
' Loop through files in folder.
Do While strFileName <> ""
' Open each file in folder
Workbooks.Open strFolderPath & strFileName
With ActiveWorkbook
' Loop through each worksheet in ActiveWorkbook
For Each ws In .Worksheets
ws.Cells.Replace What:=fnd, Replacement:=rep, _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next
.Close True
End With
strFileName = Dir()
' Exclude this workbook.
'If ThisWorkbook.Name = strFileName Then strFileName = Dir()
Loop
'Message Box when tasks are completed
MsgBox "Complete!"
ProcedureExit:
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.