Search VBA code for PDF

Tofik

Board Regular
Joined
Feb 4, 2021
Messages
114
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi guys who can help me to create the VBA code to search PDF files for example in location (C:/New Folder 2)
I need to create excel in which I input file and have results from searching below.

AAAA15.xlsx
ABCDEFG
1
2Find:21-PIP-ISO-08502-0006Send to folder ( C:\New Folder 1 )button(Active X)
3Last date file was modified ( Date modified )
41HS523217-21-PIP-ISO-08502-0006_RM01B.pdf3/30/2022
52HS523217-21-PIP-ISO-08502-0006_RM01A.pdf3/11/2022
63HS523217-21-PIP-ISO-08502-0006_2.pdf1/13/2022
74HS523217-21-PIP-ISO-08502-0006_1B.pdf1/6/2022
85HS523217-21-PIP-ISO-08502-0006_1A.pdf10/27/2021
96HS523217-21-PIP-ISO-08502-0006_1.pdf7/12/2021
107
118
123
1310
14
15
Sheet1


1648639921650.png
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try this macro. It reads the search folder path from B1, e.g. C:\New Folder 2\
VBA Code:
Public Sub Find_PDF_Files()

    Dim folder As String, findFile As String, file As String
    Dim n As Long
    Dim destCell As Range
   
    With ActiveSheet
        folder = Trim(.Range("B1").Value)
        findFile = .Range("B2").Value
        Set destCell = .Range("A4:C4")
       
        n = .Cells(.Rows.Count, "A").End(xlUp).Row
        If n >= 4 Then destCell.Resize(n - destCell.Row + 1).ClearContents
   
        If Right(folder, 1) <> "\" Then folder = folder & "\"
        n = 0
        file = Dir(folder & "*" & findFile & "*.pdf")
        While file <> vbNullString
            destCell.Offset(n).Value = Array(n + 1, file, FileDateTime(folder & file))
            n = n + 1
            file = Dir
        Wend
       
        .Columns("C").NumberFormat = "m/d/yyyy"
    End With
   
End Sub
 
Upvote 0
Solution
Try this macro. It reads the search folder path from B1, e.g. C:\New Folder 2\
VBA Code:
Public Sub Find_PDF_Files()

    Dim folder As String, findFile As String, file As String
    Dim n As Long
    Dim destCell As Range
 
    With ActiveSheet
        folder = Trim(.Range("B1").Value)
        findFile = .Range("B2").Value
        Set destCell = .Range("A4:C4")
     
        n = .Cells(.Rows.Count, "A").End(xlUp).Row
        If n >= 4 Then destCell.Resize(n - destCell.Row + 1).ClearContents
 
        If Right(folder, 1) <> "\" Then folder = folder & "\"
        n = 0
        file = Dir(folder & "*" & findFile & "*.pdf")
        While file <> vbNullString
            destCell.Offset(n).Value = Array(n + 1, file, FileDateTime(folder & file))
            n = n + 1
            file = Dir
        Wend
     
        .Columns("C").NumberFormat = "m/d/yyyy"
    End With
 
End Sub
[/CODEHi
[/QUOTE]

Thanks. [/B]

Hi John , thanks for your VBA code. As I understood it is only Search VBA code without special button which send needed searched document from the result list in to the folder different folder. ( Example if I have result B4 B5 B6 B7 B8 B9 after searching from folder NewFolder1(which have all scope of documents),and I want take only needed document (only B5 for example) and send the copy to the NewFolder2.

And the main Question from your VBA where I should correct and specify the path to the folder from your code ?
 
Upvote 0
As I understood it is only Search VBA code without special button which send needed searched document from the result list in to the folder different folder. ( Example if I have result B4 B5 B6 B7 B8 B9 after searching from folder NewFolder1(which have all scope of documents),and I want take only needed document (only B5 for example) and send the copy to the NewFolder2.
Yes, the macro only searches the folder for the partial file name in B2 and puts the search results in A4:C4 down. You didn't say anything about copying one of the found files to another folder, which will be another macro. How do you want to specify which of the found files should be copied to the other folder? How will you specify this folder?

And the main Question from your VBA where I should correct and specify the path to the folder from your code ?
As the code is written, you should specify the folder path in cell B1. If you wish, I can change it to open a folder browser for you to choose the search folder.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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