anewman5high
New Member
- Joined
- Aug 25, 2017
- Messages
- 11
Hello,
I'm trying to put together a VBA button to search all of the workbooks in a specified folder for a user inputted word in any cell (usually column A if that makes it easier). Once found I'd like it to copy the whole row to a new sheet in the Workbook the VBA is run from. Currently I have frankensteined together the below code from Google searches. It lists the Name of the workbook and sheet the text was found in and then the address and contained text of the cell it was found in. I'd like to keep these if possible but then copy the rest of the row from column 5 onwards.
I'm sure I'm doing something very basic wrong but just can't seem to get this bit right! Also happy to throw away all of the below code if something else works better!
Thanks in advance for any help, apologies if I've explained myself badly!
Alan
I'm trying to put together a VBA button to search all of the workbooks in a specified folder for a user inputted word in any cell (usually column A if that makes it easier). Once found I'd like it to copy the whole row to a new sheet in the Workbook the VBA is run from. Currently I have frankensteined together the below code from Google searches. It lists the Name of the workbook and sheet the text was found in and then the address and contained text of the cell it was found in. I'd like to keep these if possible but then copy the rest of the row from column 5 onwards.
I'm sure I'm doing something very basic wrong but just can't seem to get this bit right! Also happy to throw away all of the below code if something else works better!
Code:
Sub SearchFolders()
Dim fso As Object
Dim fld As Object
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim lRow As Long
Dim rFound As Range
Dim strFirstAddress As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
strPath = "C:\Searchfolderhere\"
strSearch = InputBox("Search Term")
Set wOut = Worksheets.Add
lRow = 1
With wOut
.Cells(lRow, 1) = "Workbook"
.Cells(lRow, 2) = "Worksheet"
.Cells(lRow, 3) = "Cell"
.Cells(lRow, 4) = "Text in Cell"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder(strPath)
strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
Set wbk = Workbooks.Open _
(Filename:=strPath & "\" & strFile, _
UpdateLinks:=0, _
ReadOnly:=True, _
AddToMRU:=False)
For Each wks In wbk.Worksheets
Set rFound = wks.UsedRange.Find(strSearch)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
End If
Do
If rFound Is Nothing Then
Exit Do
Else
lRow = lRow + 1
.Cells(lRow, 1) = wbk.Name
.Cells(lRow, 2) = wks.Name
.Cells(lRow, 3) = rFound.Address
.Cells(lRow, 4) = rFound.Value
End If
Set rFound = wks.Cells.FindNext(After:=rFound)
Loop While strFirstAddress <> rFound.Address
Next
wbk.Close (False)
strFile = Dir
Loop
.Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"
ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
Thanks in advance for any help, apologies if I've explained myself badly!
Alan