Import multiple photos from folders in columns

Qba

New Member
Joined
Oct 25, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,
I'm working on some code to import multiple images into one sheet and I stumble to modify the code from 2022 thread (import multiple photos from a folders then resize) to change it's behavior slightly.

The code:
VBA Code:
Public Sub insertpics()

    Dim folders() As String, foldersCount As Long
    Dim showOK As Boolean
    Dim fileName As String
    Dim i As Long
    Dim pic As Shape
    Dim picRange As Range
    Dim xSht As Worksheet
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        foldersCount = 0
        Do
            .Title = "Select images folder " & foldersCount + 1 & " or click Cancel to " & IIf(foldersCount = 0, "exit macro", "insert images")
            showOK = .Show
            If showOK Then
                ReDim Preserve folders(0 To foldersCount)
                folders(foldersCount) = .SelectedItems(1) & "\"
                foldersCount = foldersCount + 1
            End If
        Loop Until Not showOK
    End With
    If foldersCount = 0 Then Exit Sub
    
    
    Set xSht = ThisWorkbook.Worksheets("agrE")
    Set picRange = xSht.Range("B12")
    
    For i = 0 To UBound(folders)
        fileName = Dir(folders(i) & "*.*")
        While fileName <> vbNullString
            If InStr(1, "|.jpg|.png.|.bmp|", "|" & Mid(fileName, InStrRev(fileName, ".")) & "|", vbTextCompare) Then
                With picRange
                    Set pic = .Worksheet.Shapes.AddPicture(fileName:=folders(i) & fileName, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
                                                           Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
                    Set picRange = .Offset(, .Columns.Count)
                End With
            End If
            fileName = Dir
        Wend
    Next
    
End Sub

I would like and frankly I need the code to import the pictures from one folder in rows begining at B12 and then the next folder should populate rows from C12 and so on.
Basically every folder in seperate columns.

How can I modify the code to accomplish this?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi and welcome to MrExcel

Try:

VBA Code:
Public Sub insertpics()
  Dim folders() As String, foldersCount As Long
  Dim i As Long, j As Long, k As Long
  Dim fileName As String
  Dim xSht As Worksheet
  Dim showOK As Boolean
  Dim pic As Shape
  
  With Application.FileDialog(msoFileDialogFolderPicker)
    foldersCount = 0
    Do
      .Title = "Select images folder " & foldersCount + 1 & " or click Cancel to " & IIf(foldersCount = 0, "exit macro", "insert images")
      showOK = .Show
      If showOK Then
        ReDim Preserve folders(0 To foldersCount)
        folders(foldersCount) = .SelectedItems(1) & "\"
        foldersCount = foldersCount + 1
      End If
    Loop Until Not showOK
  End With
  If foldersCount = 0 Then Exit Sub
  
  Set xSht = ThisWorkbook.Worksheets("agrE")
  k = 12
  j = 2
  
  For i = 0 To UBound(folders)
    fileName = Dir(folders(i) & "*.*")
    While fileName <> vbNullString
      If InStr(1, "|.jpg|.png.|.bmp|", "|" & Mid(fileName, InStrRev(fileName, ".")) & "|", vbTextCompare) Then
        With xSht.Cells(k, j)
          Set pic = .Worksheet.Shapes.AddPicture(fileName:=folders(i) & fileName, LinkToFile:=msoFalse, _
            SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
          k = k + 1
        End With
      End If
      fileName = Dir
    Wend
    k = 12
    j = j + 1
  Next
End Sub
 
Upvote 0
Hi,
thank you. Your reply was so fast but I won't be able to test changes till monday when I'm back in the office. I will definetely reply with some feedback then.

Meanwhile thank you for you help and enjoy the weekend.
 
Upvote 0

Forum statistics

Threads
1,224,884
Messages
6,181,552
Members
453,052
Latest member
ezzat

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