Forum Discussion
filipiovitu
May 01, 2024Copper Contributor
Search for a value in entire workbook, return the entire row where value was found
Hello All, Example: I have 10 Invoice numbers (C093705926 C093705927 C093725527 C093750757 C093750759 C093769955 C093769956 C093789382 C093808525 C093808526) and need to search these in a workboo...
- May 01, 2024
Try this macro:
Sub ListEm() Dim wbkSource As Workbook Dim wshSource As Worksheet Dim rngSource As Range Dim strAddress As String Dim wbkTarget As Workbook Dim wshTarget As Worksheet Dim varSearch As Variant Dim lngRow As Long Application.ScreenUpdating = False Set wbkSource = ActiveWorkbook Set wbkTarget = Workbooks.Add(xlWBATWorksheet) Set wshTarget = wbkTarget.Worksheets(1) For Each wshSource In wbkSource.Worksheets For Each varSearch In Array("C093705926", "C093705927", "C093725527", _ "C093750757", "C093750759", "C093769955", "C093769956", _ "C093789382", "C093808525", "C093808526") Set rngSource = wshSource.Range("C:C").Find(What:=varSearch, LookAt:=xlWhole) If Not rngSource Is Nothing Then strAddress = rngSource.Address Do lngRow = lngRow + 1 rngSource.EntireRow.Copy Destination:=wshTarget.Range("A" & lngRow) Set rngSource = wshSource.Range("C:C").Find(What:=varSearch, _ After:=rngSource, LookAt:=xlWhole) If rngSource Is Nothing Then Exit Do Loop Until rngSource.Address = strAddress End If Next varSearch Next wshSource Application.CutCopyMode = False Application.ScreenUpdating = True End Sub
HansVogelaar
May 01, 2024MVP
- filipiovituMay 01, 2024Copper ContributorThe Invoice Numbers are located in Column C in sheets in the same workbook,
the output should be a literal copy of the row, which is 39 cells long- HansVogelaarMay 01, 2024MVP
Thanks. How many sheets do you want to search, and is this fixed or variable?
- filipiovituMay 01, 2024Copper Contributor
I'd say it would be around 4-5 sheets, but this is variable since I might need to search through more, maybe11-12.