Amend search userform to search another file.

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have this code that searches for data entry of the process order entered in textbox1.

What I would like is to expand the search to include previous years entries which are on another file and numerous worksheets.

This is the other file: C:\Users\Dell\Desktop\PO Entry Copy.xlsm

and the worksheet names are: 2017, 2018, 2019, 2020 up to 2025.

Here is the code:
Code:
Private Sub CommandButton1_Click()Dim at As Long, LR As Long, x As Long, j As Long, val As Double


If TextBox1.Value = "" Or TextBox1.Value = "ENTER THE PROCESS ORDER NUMBER HERE" Then
MsgBox "Please enter a Process Order number!"
Exit Sub
End If


val = TextBox1
With Sheets("Changeover Form")
    at = Application.CountIf(.Range("A:A"), TextBox1)
    If at > 0 Then
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        ReDim arr(1 To LR, 1 To 5)
        j = 0
        For x = 3 To LR
            If .Range("A" & x).Value = val Then
                j = j + 1
                arr(j, 1) = .Range("A" & x).Value
                arr(j, 2) = .Range("B" & x).Value
                arr(j, 3) = .Range("C" & x)
                arr(j, 4) = Format(.Range("I" & x), "dd/mm/yyyy hh:mm:ss")
                arr(j, 5) = .Range("A" & x).Row
            End If
        Next
        ListBox1.List = arr
        For x = ListBox1.ListCount - 1 To 0 Step -1
            If ListBox1.List(x) = "" Then
                ListBox1.RemoveItem (x)
            End If
        Next
    Else
        MsgBox "INCORRECT DATA ENTRY" & vbCrLf & vbCrLf & "Please check your PO number and try again", vbExclamation, "Palletiser Operator"
        
        ListBox1.SetFocus
        
        Exit Sub
    End If
End With
End Sub

any help would be much appreciated.

Thanks

Dan
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Seems like this would work...
Code:
Private Sub CommandButton1_Click()
Dim at As Long, LR As Long, x As Long, j As Long, val As Double
Dim FilDir As Object, FSO As Object, Cnt As Integer
If TextBox1.Value = "" Or TextBox1.Value = "ENTER THE PROCESS ORDER NUMBER HERE" Then
MsgBox "Please enter a Process Order number!"
Exit Sub
End If
On Error GoTo Erfix
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set FSO = CreateObject("scripting.filesystemobject")
'***change File path to your file
Set FilDir = FSO.GetFile("C:\Users\Dell\Desktop\PO Entry Copy.xlsm")
Workbooks.Open FileName:=FilDir
For Cnt = 2017 To 2025
val = TextBox1
With Sheets(CStr(Cnt))
    at = Application.CountIf(.Range("A:A"), TextBox1)
    If at > 0 Then
        LR = .Range("A" & Rows.Count).End(xlUp).Row
        ReDim Arr(1 To LR, 1 To 5)
        j = 0
        For x = 3 To LR
            If .Range("A" & x).Value = val Then
                j = j + 1
                Arr(j, 1) = .Range("A" & x).Value
                Arr(j, 2) = .Range("B" & x).Value
                Arr(j, 3) = .Range("C" & x)
                Arr(j, 4) = Format(.Range("I" & x), "dd/mm/yyyy hh:mm:ss")
                Arr(j, 5) = .Range("A" & x).Row
            End If
        Next
        ListBox1.List = Arr
        For x = ListBox1.ListCount - 1 To 0 Step -1
            If ListBox1.List(x) = "" Then
                ListBox1.RemoveItem (x)
            End If
        Next
    Else
        MsgBox "INCORRECT DATA ENTRY" & vbCrLf & vbCrLf & "Please check your PO number and try again", vbExclamation, "Palletiser Operator"
        ListBox1.SetFocus
        Exit Sub
    End If
End With
Next Cnt
Erfix:
Workbooks(FilDir.Name).Close SaveChanges:=False
Set FilDir = Nothing
Set FSO = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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