I found a code that searches multiple workbooks for a string ("James")and returns an output of:

  1. the workbook name that the string was found,
  2. the sheet
  3. the cell
  4. and the string it was searching for ("James")

I want the code to return the row entries in which the string is found instead of just the string in output number 4. Can you please help me edit the code?

Source of code: https://www.extendoffice.com/documents/excel/3354-excel-search-multiple-sheets-workbooks.html

      Dim xFso As Object
    Dim xFld As Object
    Dim xStrSearch As String
    Dim xStrPath As String
    Dim xStrFile As String
    Dim xOut As Worksheet
    Dim xWb As Workbook
    Dim xWk As Worksheet
    Dim xRow As Long
    Dim xFound As Range
    Dim xStrAddress As String
    Dim xFileDialog As FileDialog
    Dim xUpdate As Boolean
    Dim xCount As Long
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a forlder"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    xStrSearch = "James"
    xUpdate = Application.ScreenUpdating
    Application.ScreenUpdating = False
    Set xOut = Worksheets.Add
    xRow = 1
    With xOut
        .Cells(xRow, 1) = "Workbook"
        .Cells(xRow, 2) = "Worksheet"
        .Cells(xRow, 3) = "Cell"
        .Cells(xRow, 4) = "Text in Cell"
        Set xFso = CreateObject("Scripting.FileSystemObject")
        Set xFld = xFso.GetFolder(xStrPath)
        xStrFile = Dir(xStrPath & "\*.xls*")
        Do While xStrFile <> ""
            Set xWb = Workbooks.Open(Filename:=xStrPath & "\" & xStrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
            For Each xWk In xWb.Worksheets
                Set xFound = xWk.UsedRange.Find(xStrSearch)
                If Not xFound Is Nothing Then
                    xStrAddress = xFound.Address
                End If
                Do
                    If xFound Is Nothing Then
                        Exit Do
                    Else
                        xCount = xCount + 1
                        xRow = xRow + 1
                        .Cells(xRow, 1) = xWb.Name
                        .Cells(xRow, 2) = xWk.Name
                        .Cells(xRow, 3) = xFound.Address
                        .Cells(xRow, 4) = xFound.Value
                    End If
                    Set xFound = xWk.Cells.FindNext(After:=xFound)
                Loop While xStrAddress <> xFound.Address
            Next
            xWb.Close (False)
            xStrFile = Dir
        Loop
        .Columns("A:D").EntireColumn.AutoFit
    End With
    MsgBox xCount & "cells have been found", , "Kutools for Excel"
ExitHandler:
    Set xOut = Nothing
    Set xWk = Nothing
    Set xWb = Nothing
    Set xFld = Nothing
    Set xFso = Nothing
    Application.ScreenUpdating = xUpdate
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub

Current code result: Current code output

How I want it to look after code update:

Data

1 Answers

0
QuickSilver On Best Solutions

Basically you need to find out the last used column in the data workbook and then just loop through the columns and write the data to the new workbook. I added xCol and i as long and did a for loop to write the data.

Option Explicit        
Sub OpenWBCopyData()
        Dim xFso As Object
        Dim xFld As Object
        Dim xStrSearch As String
        Dim xStrPath As String
        Dim xStrFile As String
        Dim xOut As Worksheet
        Dim xWb As Workbook
        Dim xWk As Worksheet
        Dim xRow As Long
        Dim xCol as Long
        Dim i as Long
        Dim xFound As Range
        Dim xStrAddress As String
        Dim xFileDialog As FileDialog
        Dim xUpdate As Boolean
        Dim xCount As Long

        On Error GoTo ErrHandler

        Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
        xFileDialog.AllowMultiSelect = False
        xFileDialog.Title = "Select a forlder"
        If xFileDialog.Show = -1 Then
            xStrPath = xFileDialog.SelectedItems(1)
        End If
        If xStrPath = "" Then Exit Sub
        xStrSearch = "James"
        xUpdate = Application.ScreenUpdating
        Application.ScreenUpdating = False
        Set xOut = Worksheets.Add
        xRow = 1
        With xOut
            .Cells(xRow, 1) = "Workbook"
            .Cells(xRow, 2) = "Worksheet"
            .Cells(xRow, 3) = "Cell"
            .Cells(xRow, 4) = "Text in Cell"
            Set xFso = CreateObject("Scripting.FileSystemObject")
            Set xFld = xFso.GetFolder(xStrPath)
            xStrFile = Dir(xStrPath & "\*.xls*")
            Do While xStrFile <> ""
                Set xWb = Workbooks.Open(Filename:=xStrPath & "\" & xStrFile, UpdateLinks:=0, ReadOnly:=True, AddToMRU:=False)
                For Each xWk In xWb.Worksheets
                    Set xFound = xWk.UsedRange.Find(xStrSearch)
                    If Not xFound Is Nothing Then
                        xStrAddress = xFound.Address
                        xCol = xWk.xFound(xFound.Cell & .Columns.Count).End(xlToLeft).Column
                    End If
                    Do
                        If xFound Is Nothing Then
                            Exit Do
                        Else
                            xCount = xCount + 1
                            xRow = xRow + 1
                            .Cells(xRow, 1) = xWb.Name
                            .Cells(xRow, 2) = xWk.Name
                            .Cells(xRow, 3) = xFound.Address
                            .Cells(xRow, 4) = xFound.Value
                            For i = 1 To xCol
                               .Cells(xRow, 4 + i) = xFound.Value
                            Next i
                        End If
                        Set xFound = xWk.Cells.FindNext(After:=xFound)
                    Loop While xStrAddress <> xFound.Address
                Next
                xWb.Close (False)
                xStrFile = Dir
            Loop
            .Columns("A:D").EntireColumn.AutoFit
        End With
        MsgBox xCount & "cells have been found", , "Kutools for Excel"
    ExitHandler:
        Set xOut = Nothing
        Set xWk = Nothing
        Set xWb = Nothing
        Set xFld = Nothing
        Set xFso = Nothing
        Application.ScreenUpdating = xUpdate
        Exit Sub
    ErrHandler:
        MsgBox Err.Description, vbExclamation
        Resume ExitHandler
End Sub