Check if a cell content has a match in a folder and if yes add a designator to corresponding cell

zakasnak

Active Member
Joined
Sep 21, 2005
Messages
308
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
Hello, all!

It's been awhile since I've posted, but this one is challenging me.

I have a sheet with unique values for documents in column A, 10 char, header row 1. I would like to run a search on a network folder (S:\_FY2024) to see if the document has already been processed at least once (sometimes they can be processed more than once), which would show up as the unique value at the beginning of the file name. If the document number is found in the file directory, I would like for the macro to put a designator "Y" in the same row, col K.

DocumentOrganizationTravelerTANUMTrip NameTrip EndTotal CostDays WaitingAssigned?Returned?
TV0001XXXXORGORGLname, FnameTANUM0001XXXXTrip Name4/26/2024$555.50
22​
TM

I assume it will need to be VBA, but if a formula would work, I would be happy with that as well! :D

TIA for any help anyone can give me!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
try:
VBA Code:
Option Explicit

Sub LoopThroughFilesInFolder()
    Application.ScreenUpdating = False
    Dim xDir As String, xName As String
    Dim xFSO As Object, xFolder As Object, xFile As Object
    Dim i As Long
    Dim iList As Range, cll As Range
    i = lr(ThisWorkbook.Sheets(1), 1)
    If i < 2 Then Exit Sub
    Set iList = ThisWorkbook.Sheets(1).Cells(2, 1).Resize(i - 1, 1)
    xDir = "S:\_FY2024"
    Set xFSO = CreateObject("Scripting.FileSystemObject")
    Set xFolder = xFSO.GetFolder(xDir)
    For Each xFile In xFolder.Files
        xName = xFSO.GetFileName(xFile)
        Set cll = Nothing
        Set cll = iList.Find(What:=Left(xName, 10), LookIn:=xlValues, LookAt:=xlWhole)
        If Not cll Is Nothing Then cll.Offset(, 10).Value = "Y"
        Application.StatusBar = "Comparing: " & xName
        DoEvents
    Next xFile
    Application.StatusBar = False
    Set xFile = Nothing
    Set xFolder = Nothing
    Set xFSO = Nothing
    Application.ScreenUpdating = True
End Sub

Private Function lr(ByVal ws As Worksheet, ByVal icol As Integer) As Long
    lr = ws.Cells(Rows.Count, icol).End(xlUp).Row
End Function
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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