Sub LoopThroughFiles()
Dim myID As String
Dim myResponse As String
Dim Sh As Worksheet
Dim Loc As Range
Dim FirstFound As String
Dim counter As Long
myID = Application.InputBox("Enter Customer ID", "Customer ID", Type:=2)
If myID = "False" Then Exit Sub 'user canceled
myResponse= Application.InputBox("Enter the comment to be entered in all instances", "Comment", Type:=2)
If myResponse= "False" Then Exit Sub 'user canceled
Dim xFdItem As String
Dim xFileName As String
'Select Folder containing workbooks
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
xFdItem = .SelectedItems(1) & Application.PathSeparator
Else: Exit Sub 'user canceled
End If
End With
Application.ScreenUpdating = False
xFileName = Dir(xFdItem & "*.xlsx")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
'What to do in each workbook
For Each Sh In .Worksheets
Set Loc = Sh.UsedRange.Cells.Find(myID, , xlValues, xlWhole, 1, 1, 0)
If Not Loc Is Nothing Then
FirstFound = Loc.Address
Do
Loc.Offset(0, 3).Value = myResponse
counter = counter + 1
Set Loc = Sh.UsedRange.FindNext(Loc)
Loop Until Loc.Address = FirstFound
End If
Next Sh
'End of routine within each workbook
.Close SaveChanges:=True
End With
xFileName = Dir
Loop
Application.ScreenUpdating = True
MsgBox counter & " entries edited.", vbInformation, "ID: " & myID
End Sub
Bookmarks