简体   繁体   中英

Determine if a file exists within a folders file-path that is contained in a Cell on Excel for a range of cells?

I am keen to add functionality to an excel spreadsheet I am building to track compliance.

My idea is proving hard to achieve and after reading loads of articles and posts, I'm very confused.

I have a spreadsheet that has 5 columns, one of these columns will contain filepaths for around 100 folders.

The other columns will be files that need to be in these folders - 4 files with the same reference in their titles.

I would like to press a button, and have the Sub run on startup to go through each of these folders and ensure the compliance files are present. I have tried to adapt some code, but I'm struggling with a few things.

  1. How do I actually do this?
  2. How do use the dir() funtion to pull a filepath from a cell?
  3. How do I reference the cell next to the cell currently being investigated
  4. How do I cycle this through a Range of cells - Ie (A2:A200)
  5. It will need to read from the A column, but return a Boolean value (1 or 0) to a different cell on the same row, how will I accomplish this.

Here's where I got to, this is some code I found online.


Sub IsItThere()

Dim Cell As Range
Dim fileName As Variant
Dim i As Integer
Dim C As String

Cell = E3: E200  'The range of Filepaths
For i = 3 To 200
Do

C = Cell(i)

fileName = Dir(C)
While fileName <> ""
    
    If fileName = Dir()(C + "January*") Then   'Does it contain the key phrase
    B(i) = "1"
Else
    B(i) = "0"         'Make the cell next door to the cell with the current filepath become 1

 End If
i = i + 1
    fileName = Dir
Wend

End Sub

This simple code will get text from each cell in range A2:A200 on active sheet, check that last symbol in this string is a Path Separator and add it if need.

Then for four cells at right check that file exists and put result (TRUE or FALSE) to cells in columns F:I

Sub IsItThere()
Dim rCell As Range
Dim sPath As String
Dim sFileName As String
Dim i As Integer
    For Each rCell In [A2:A200].Cells()
        sPath = rCell.Text
        If Right(sPath, 1) <> Application.PathSeparator Then sPath = sPath & Application.PathSeparator
        For i = 1 To 4
            sFileName = Trim(rCell.Offset(0, i).Text)
            rCell.Offset(0, i + 4) = (Dir(sPath & sFileName) <> vbNullString)
        Next i
    Next rCell
End Sub

So result will be like this: 结果示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM