Search Data

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Option Explicit

Sub SearchData()
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wksReport As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim startRow As Long
Dim rngFound As Range
Dim strFirstAddress As String

Application.ScreenUpdating = False

strPath = "D:\CDR DATABASE"


strSearch = Application.InputBox("Enter the text to Search", "My choice
of text", "Enter Search Text", vbOKCancel)
If strSearch = "False" Then Exit Sub

Set wksReport = Worksheets.Add


startRow = 1

With wksReport
.Cells(startRow, 1) = "WORKBOOK"
.Cells(startRow, 2) = "WORKSHEET"
.Cells(startRow, 3) = "CELL ADDRESS"
.Cells(startRow, 4) = "TEXT FOUND"

strFile = Dir(strPath & "\ *.xls*")

Do While strFile <> ""


Set wbk = Workbooks.Open(Filename:= strPath & "\" & strFile)

For Each wks In wbk.Worksheets

Set rngFound = wks.UsedRange.Find(strSearch)

If Not rngFound is Nothing Then


StrFirstAddress = rngFound.Address
End If

Do
If rngFound is Nothing Then
Exit Do
Else
startRow = startRow + 1
.Cells(startRow, 1) = wbk.Name
.Cells(startRow, 2) = wks.Name
.Cells(startRow, 3) = rngFound.Address
.Cells(startRow, 4) = rngFound.Value
End If

Set rngFound = wks.Cells.FindNext(After:=rngFound)

Loop While strFirstAddress <> rngFound. Address

Next

wbk.Close (False)
strFile = Dir
Loop

.Columns("A:D").EntireColumn.AutoFit
End With
If wksReport.Cells(2,1) = "" Then

MsgBox "All workbooks in folder has been searched!!!", vbCrLf & "No
Data Found...!!!"

Cells(startRow, 1) = ""
Cells(startRow, 2) = ""
Cells(startRow, 3) = ""
Cells(startRow, 4) = ""

On Error Resume Next

Application.DisplayAlerts = False

wksReport.Delete

Else

MsgBox = "All workbooks in folder has been searched!!!", vbCrLf &


"Data Found and extracted...!!!"

End If

Set wksReport = Nothing


Set wks = Nothing
Set wbk = Nothing

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

You might also like