List Picture Names Of A Folder In Excel With VBA
In Excel, there's no built-in function which will automatically list all picture names of a folder you select during a cell of a sheet, but here I even have a VBA code which may assist you quickly solve this job.
1. In Excel, then press Alt + F11 keys to open Microsoft Visual Basic for Applications window.
2. In popping window, click Insert > Module to create a new Module script.
3. Copy below code and paste them to the new Module script window.
Sub PictureNametoExcel()
'UpdatebyExtendoffice
Dim I As Long
Dim xRg As Range
Dim xAddress As String
Dim xFileName As String
Dim xFileDlg As FileDialog
Dim xFileDlgItem As Variant
On Error Resume Next
xAddress = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Select a cell to place name list:", "Kutools For Excel", Address, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set xRg = xRg(1)
xRg.Value = "Picture Name"
With xRg.Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 10
End With
xRg.EntireColumn.AutoFit
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
I = 1
If xFileDlg.Show = -1 Then
xFileDlgItem = xFileDlg.SelectedItems.Item(1)
xFileName = Dir(xFileDlgItem & "\")
Do While xFileName <> ""
If InStr(1, xFileName, ".jpg") + InStr(1, xFileName, ".png") + InStr(1, xFileName, ".img") + InStr(1, xFileName, ".ioc") + InStr(1, xFileName, ".bmp") > 0 Then
xRg.Offset(I).Value = xFileDlgItem & "\" & xFileName
I = I + 1
End If
xFileName = Dir
Loop
End If
Application.ScreenUpdating = True
End Sub
4. Press F5 key, then a dialog pops out for you to select a cell to place the picture names.
5. Click OK to continue selecting the folder you want to list the picture names in Browse dialog.
Works like a charm. Thanks a Lot
ReplyDelete