Hello,
I am trying to put together a VBA code to search all of the workbooks in a specific folder (combine) based on user text criteria from an input box (Net Income). When the code finds it, I would like to copy the whole row into a new sheet.
My current issue is that I cannot figure out how to search all columns of all workbooks, other than just column A, and return the row data as a block where I have the name of the Workbook, name of the Worksheet, Cell position, Text in Cell (user text criteria), then the row data (see screenshots attached - Code expectation). Basically, if the text criteria entered is not in column A, the code does not copy the row based on the criteria's position or sometimes does it but leave spaces between the Text in Cell and where the row data should be (see screenshot).
I am sure there is a better solution or a way to fix this code for this code. In advance, thanks for any help and for your time. It would be a great learning experience. Feel free to reach out if I haven't explained myself well. Thanks!
>>> use code - tags <<<
I am trying to put together a VBA code to search all of the workbooks in a specific folder (combine) based on user text criteria from an input box (Net Income). When the code finds it, I would like to copy the whole row into a new sheet.
My current issue is that I cannot figure out how to search all columns of all workbooks, other than just column A, and return the row data as a block where I have the name of the Workbook, name of the Worksheet, Cell position, Text in Cell (user text criteria), then the row data (see screenshots attached - Code expectation). Basically, if the text criteria entered is not in column A, the code does not copy the row based on the criteria's position or sometimes does it but leave spaces between the Text in Cell and where the row data should be (see screenshot).
I am sure there is a better solution or a way to fix this code for this code. In advance, thanks for any help and for your time. It would be a great learning experience. Feel free to reach out if I haven't explained myself well. Thanks!
>>> use code - tags <<<
Code:
Dim strFirstAddress As String
Dim strSearch As String
Const strPath As String = "/Users/xxx/Desktop/combine/"
ChDir strPath
strExtension = Dir("*.xlsx*") 'File type can be modified to fit csv files
strSearch = InputBox("Please enter the Search Keyword.") 'Search Keyword all reports
Set wOut = Worksheets.Add
wOut.Range("A1:D1") = Array("Workbook", "Worksheet", "Cell", "Text in Cell")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath & strExtension)
With wkbSource
For Each wks In .Sheets
Set rFound = wks.Range("A:A").Find(strSearch, LookIn:=xlValues, lookat:=xlWhole)
If Not rFound Is Nothing Then
strFirstAddress = rFound.Address
Do
wOut.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = wkbSource.Name
wOut.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = wks.Name
wOut.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) = rFound.Address
wOut.Cells(Rows.Count, "D").End(xlUp).Offset(1, 0) = rFound.Value
wks.Range(wks.Cells(rFound.Row, 1), wks.Cells(rFound.Row, wks.Cells(rFound.Row, wks.Columns.Count).End(xlToLeft).Column)).Copy wOut.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0)
Set rFound = wks.Range("A:A").FindNext(rFound)
Loop While rFound.Address <> strFirstAddress
sAddr = ""
End If
Next wks
End With
wkbSource.Close savechanges:=False
strExtension = Dir
Loop
Application.ScreenUpdating = True
End Sub
Attachments
Last edited by a moderator: