VBA check if full or partial name exists in folder

Holyfix

New Member
Joined
Oct 14, 2021
Messages
9
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hey everyone,
I am struggling with VBA code. I have a folder with pdf files. Every file contains one or more documents. These pdf files are named accordignly to the documents inside so I may have PDF file named 8947 (one document inside) or 8468 7638 7364 (three documents inside). Also, I have an Excel sheet with column containing these numbers and i need to check if every number (PDF file) exists in the folder or not. Is there a way to write VBA code which gives me YES or NO next to these numbers in Excel ?

Thank you very much
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Holyfix and Welcome to the Board! You didn't indicate the column where the file names are located or where U want the "Yes" or "No"? Anyways, the code below assumes sheet1 column "A" has the file names and "B" is the output. You will need to adjust the folder path, sheet name and columns to suit. HTH. Dave
Code:
Private Sub tester()
Dim FSO As Object, FolDir As Object, FileNm As Object, LastRow As Integer
Dim Flag As Boolean, WS As Worksheet, Cnt As Integer
Set FSO = CreateObject("scripting.filesystemobject")
'*****change folderpath to suit
Set FolDir = FSO.GetFolder(ThisWorkbook.Path & "\Datafiles2")
Set WS = Sheets("sheet1")
With WS
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
For Cnt = 1 To LastRow
Flag = False
For Each FileNm In FolDir.Files
If FileNm.Name Like "*" & ".pdf" Then
If InStr(FileNm.Name, CStr(WS.Cells(Cnt, "A"))) Then
WS.Cells(Cnt, "B") = "YES"
Flag = True
Exit For
End If
End If
Next FileNm
If Not Flag Then
WS.Cells(Cnt, "B") = "NO"
End If
Next Cnt
Set FolDir = Nothing
Set FSO = Nothing
End Sub
 
Upvote 0
Same idea

VBA Code:
Sub jec()
 Dim i As Long
 With Sheets("sheet1")
   For i = 1 To .Range("A" & Rows.Count).End(xlUp).Row
     .Cells(i, 2) = Dir(ThisWorkbook.Path & "\" & .Cells(i, 1) & "*.pdf") <> ""
   Next
 End With
End Sub
 
Upvote 0
Hello again, I just found out that it only seems to be working when you make a completely new excel. After I save it and run again, it gives me "Run-time error 76 (Path not found)". May I please ask you to help me with this ? Thank you
 
Upvote 0
Save the new workbook first, otherwise thisworkbook.path can not be found
 
Upvote 0
When I make new workbook, save it, reopen and use the code it still gives me "Path cannot be found". Seems like I can't use it on already existing file.
 
Upvote 0
It is also important where to save it
 
Upvote 0
Doesn't work even if I save it into the folder where rest of the PDFs are.
 
Upvote 0

Forum statistics

Threads
1,215,796
Messages
6,126,964
Members
449,350
Latest member
Sylvine

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