File Search Function

jaywestling

New Member
Joined
Jul 25, 2018
Messages
7
I am looking for search function(in a simple user interface) that will search through multiple excel files in a specific folder (they are invoices) . I would like to search for specific part# and if it is found, to list every instance it is found (in any/every file) with its quantity and unit price. if not found then just a message saying so.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hello. It is certainly possible to write a macro that will look inside all excel workbooks in a folder, search the sheets and cells for a specific value, and then log the results of the search. However, you need to provide more details, such as:

Do all worksheets get searched, or only particular sheets (if so, which ones)?
Which columns or cells in those sheets should be searched for the 'part number'?
Which columns or cells hold the values for 'quantity' and 'unit price'?
Where do you want to log the results of the search? (E.g. to a text file, to a separate workbook, etc.)
 
Upvote 0
Thank you so much for your reply ParamRay the in formation is as follows... I would like the search result to be output to the same workbook that the search function is in. This way it is acting kind of like a dashboard to search my invoices. My invoices are a single worksheet setup so the macro would only ever be searching worksheet#1.Part #, qty., and unit price are in adjacent . "Description" column(B16:B36) is where the part number is found. There could be up to 21 part#'s on an invoice so for instance, if the part# was found in B21, then the quantity would be in A21 and the unit price in H21 if that makes sense. So the initial search for part#'s would be looking in B16:B36. Please forgive my ignorance if I am being confusing as I am a novice at best with macros. I would like to post an image of the worksheet but I do not know how to do so here.
0ByadSoz1eZWFNjlmYnNJbW12bG8


 
Upvote 0
Hello. It is certainly possible to write a macro that will look inside all excel workbooks in a folder, search the sheets and cells for a specific value, and then log the results of the search. However, you need to provide more details, such as:

Do all worksheets get searched, or only particular sheets (if so, which ones)?
Which columns or cells in those sheets should be searched for the 'part number'?
Which columns or cells hold the values for 'quantity' and 'unit price'?
Where do you want to log the results of the search? (E.g. to a text file, to a separate workbook, etc.)


Excel 2010
ABCDEFHI
14
15QuantityDescriptionUnit PriceTotal
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Packing Slip
 
Upvote 0
You're welcome. Can already think of a bunch of things to make it better, but I'll leave it in your capable hands!

I would like one more value in the results and that is the Date (found in Cell "I3") in the worksheets being searched if that makes sense. I have been trying it by picking away at the code you gave me but to no avail. As a side note, this little bit of code has been life-changing for my business! Thank you again.
 
Upvote 0
Here is the original code you gave me...

Option Explicit
Option Private Module


Public Sub SearchButton_Click()
Dim astrWorkbooks() As String
Dim strPartNumber As String
Dim strFolderPath As String
Dim vntWorkbooks As Variant
Dim j As Long
On Error GoTo ErrHandler
If Not ValidateData("PartNumber", strPartNumber) Then
MsgBox "Part number has not been entered.", vbExclamation
Exit Sub
End If
If Not ValidateData("SearchFolder", strFolderPath) Then
MsgBox "Search folder has not been entered.", vbExclamation
Exit Sub
End If
Call ClearResultsTable
If Not FolderExists(strFolderPath) Then
MsgBox "Search folder does not exist.", vbExclamation
Exit Sub
End If
vntWorkbooks = GetAllWorkbooks(strFolderPath)
If IsEmpty(vntWorkbooks) Then
MsgBox "Search folder does not contain any Excel workbooks.", vbExclamation
Exit Sub
End If
astrWorkbooks = vntWorkbooks
For j = LBound(astrWorkbooks) To UBound(astrWorkbooks)
Call SearchWorkbook(astrWorkbooks(j), strPartNumber)
Next j
MsgBox "Search has completed. Please check results table.", vbInformation
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
End Sub


Private Function FolderExists(ByRef strFolderPath As String) As Boolean
On Error GoTo ErrHandler
If Right(strFolderPath, 1) <> Application.PathSeparator Then
strFolderPath = strFolderPath & Application.PathSeparator
End If
FolderExists = (Dir(strFolderPath, vbDirectory) <> "")
Exit Function
ErrHandler:
FolderExists = False
End Function


Private Sub ClearResultsTable()
Dim tblResults As ListObject
Dim objFilter As AutoFilter
Dim rngBody As Range
Set tblResults = wksSearchUtility.ListObjects("Results")
Set objFilter = tblResults.AutoFilter
Set rngBody = tblResults.DataBodyRange
If Not objFilter Is Nothing Then
If objFilter.FilterMode Then
objFilter.ShowAllData
End If
End If
If Not rngBody Is Nothing Then
rngBody.Delete
End If
End Sub


Private Function ValidateData(ByVal strRangeName As String, ByRef strData As String) As Boolean
On Error GoTo ErrHandler
strData = UCase(Trim(wksSearchUtility.Range(strRangeName).Text))
ValidateData = (strData <> vbNullString)
Exit Function
ErrHandler:
ValidateData = False
End Function


Private Function GetAllWorkbooks(strFolderPath As String) As Variant
Dim lngWorkbookCount As Long
Dim astrWorkbooks() As String
Dim strFileName As String
Dim strFilePath As String
On Error GoTo ErrHandler
strFileName = Dir(strFolderPath & "*.xl*")
Do Until (strFileName = vbNullString)
lngWorkbookCount = lngWorkbookCount + 1
strFilePath = strFolderPath & strFileName
ReDim Preserve astrWorkbooks(1 To lngWorkbookCount)
astrWorkbooks(lngWorkbookCount) = strFilePath
strFileName = Dir()
Loop
If lngWorkbookCount > 0 Then
GetAllWorkbooks = astrWorkbooks
Else
GetAllWorkbooks = Empty
End If
Exit Function
ErrHandler:
GetAllWorkbooks = Empty
End Function


Private Sub SearchWorkbook(strFilePath As String, strPartNumber As String)
Dim sht As Worksheet
Dim wbk As Workbook
On Error GoTo ErrHandler
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set wbk = Workbooks.Open(strFilePath, False)
For Each sht In wbk.Worksheets
Call SearchWorksheet(sht, strPartNumber)
Next sht
ExitProc:
On Error Resume Next
wbk.Close False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
ErrHandler:
Resume ExitProc
End Sub


Private Sub SearchWorksheet(sht As Worksheet, strPartNumber As String)
Dim rngTableRow As Range
Dim cell As Range
On Error GoTo ErrHandler
For Each cell In Intersect(sht.Columns("B"), sht.UsedRange).Cells
If UCase(cell.Text) Like "*" & strPartNumber & "*" Then
Set rngTableRow = GetNextRow()
rngTableRow.Item(1).Value = sht.Parent.Name
rngTableRow.Item(2).Value = sht.Name
rngTableRow.Item(3).Value = cell.Address
rngTableRow.Item(4).Value = cell.Text
rngTableRow.Item(5).Value = cell.Offset(, -1).Value
rngTableRow.Item(6).Value = cell.Offset(, 6).Value
rngTableRow.Item(7).Value = cell.Offset(, 7).Value
End If
Next cell
Exit Sub
ErrHandler:
End Sub


Private Function GetNextRow() As Range
With wksSearchUtility.ListObjects("Results")
If .InsertRowRange Is Nothing Then
Set GetNextRow = .ListRows.Add.Range
Else
Set GetNextRow = .InsertRowRange
End If
End With
End Function
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,036
Members
449,062
Latest member
mike575

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top