Hi, i found below code on a website to find specific text in a column and if text matches then copy entire row to new sheet.
Can someone please modify the above code to loop through all excel files in a folder and perform the find function on "Sheet1" of all the files and then do the copy function on "Sheet2" of a master file, so basically collecting data from all files in a folder and pasting into one master file. Also, instead of copying entire row can it only copy the adjacent cell value. For e.g say in column B it finds the text "transfer" in cell B20 then instead of copying entire row 20, can it only copy cell C20?
VBA Code:
Option Explicit
Sub SearchForString()
Dim a As Long, arr As Variant, fnd As Range, cpy As Range, addr as string
On Error GoTo Err_Execute
'populate the array for the outer loop
arr = Array("transfer", "indicate", "water")
With Worksheets("sheet1")
'outer loop through the array
For a = LBound(arr) To UBound(arr)
'locate first instance
Set fnd = .Columns("B").Find(what:=arr(a), LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not fnd Is Nothing Then
'record address of first find
addr = fnd.Address
'seed the cpy range object
If cpy Is Nothing Then Set cpy = fnd.EntireRow
Do
'build union
Set cpy = Union(cpy, fnd.EntireRow)
'look for another
Set fnd = .Columns("B").FindNext(after:=fnd)
'keep finding new matches until it loops back to the first
Loop Until fnd.Address = addr
End If
Next a
End With
With Worksheets("sheet2")
'one stop copy & paste operation
cpy.Copy Destination:=.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
Debug.Print Now & " " & Err.Number & " - " & Err.Description
End Sub
Can someone please modify the above code to loop through all excel files in a folder and perform the find function on "Sheet1" of all the files and then do the copy function on "Sheet2" of a master file, so basically collecting data from all files in a folder and pasting into one master file. Also, instead of copying entire row can it only copy the adjacent cell value. For e.g say in column B it finds the text "transfer" in cell B20 then instead of copying entire row 20, can it only copy cell C20?