简体   繁体   中英

Excel Macro to search if content of a column (filename) exists in a folder

I have an excel sheet where I have the names of jpegs in columns F & G from rows 2 to 1800. I would like to use a macro in order to see if these Jpegs exist in a folder on my computer (mac) at a certain directory (ex. /user/Dropbox/Content/productinfo/pictures). If they do exist i would like to return the value "exists", if not, then "doesnt". The only issue is in a few cases no Jpeg exists in the spreadsheet so there is nothing to look up

I'm new to excel Macros. Please help me out!

Thanks in advance!

Dim iCount As Integer
iCount = Application.WorksheetFunction.COUNTIF(Range("F2:G1800"),".jpg")

if iCount > 0 Then

  'Do your checks
End If

You can use a user-defined function for that. In you case, you can call the below using:

=fileexists("C:\MyPath\"&f2)

Here's the function

Public Function FileExists(sPath As String)

If lcase(right(sPath,5)) <> ".jpeg" Then
    FileExists = ""
Else
    If Len(Dir(sPath)) > 0 Then
        FileExists = "Exists"
    Else
        FileExists = ""
    End If
End If


End Function

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