简体   繁体   中英

VBA excel for existence of image format of type jpg

Lets say that I have a spreadsheet in excel named test.xls and inside it a column named column1 with values image1, image2, etc. And I have a folder named images, contain images of type jpg, named like the values in the column image1.jpg, image2.jpg, etc. I need a Vba code to check if the images exist in the folder based on the spreadsheet column. Thank you!

This example will display the search results in another column. Edit the variables and don't forget the last \\ in the folder path.

Sub checkFiles()
  Dim count&, lastRow&
  Dim folderPath, columnRead, columnWrite As String

  folderPath = "C:\EXAMPLE\"
  columnRead = "A"
  columnResults = "B"

  lastRow = ThisWorkbook.Sheets(1).Range(columnRead & Rows.count).End(xlUp).Row
  For count = 1 To lastRow
    Range(columnRead & count).Activate

    If Dir(folderPath & Range(columnRead & ActiveCell.Row).Value) <> "" Then
        Range(columnResults & ActiveCell.Row).Value = "File exists."
    Else
        Range(columnResults & ActiveCell.Row).Value = "File doesn't exist."
    End If

  Next count
End Sub

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