Inserting images by VBA is very slow

Robin121

Board Regular
Joined
May 6, 2011
Messages
51
Windows10, Office 2013

When inserting .gif's using the VBA-code below, it takes literally minutes to load 200+ images.
I would like to speed that up; any ideas? Thanks.

Dim c As Integer
c = 8
Do Until Cells(c, 9).Value = ""
With ActiveSheet.Pictures.Insert( _
ThisWorkbook.Path & "\Logo's\Landen\allelanden.com\" & Cells(c, 9).Value)
.Top = Cells(c, 10).Top
.Left = Cells(c, 10).Left
.Width = (.Width / .Height) * Cells(c, 10).Height
.Height = Cells(c, 10).Height
End With
c = c + 1
Loop
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Windows10, Office 2013

When inserting .gif's using the VBA-code below, it takes literally minutes to load 200+ images.
I would like to speed that up; any ideas? Thanks.

Dim c As Integer
c = 8
Do Until Cells(c, 9).Value = ""
With ActiveSheet.Pictures.Insert( _
ThisWorkbook.Path & "\Logo's\Landen\allelanden.com\" & Cells(c, 9).Value)
.Top = Cells(c, 10).Top
.Left = Cells(c, 10).Left
.Width = (.Width / .Height) * Cells(c, 10).Height
.Height = Cells(c, 10).Height
End With
c = c + 1
Loop
Does it help, for instance, to get the pictures out of the subfolder and store them directly next to the .xlsm-file, in the same folder?
 
Upvote 0
I don't imagine the location of the image files will make much of a difference, but you may want to consider turning the ScreenUpdating property off at the outset and then on again at the end. You can do this with
VBA Code:
Application.ScreenUpdating = False
At the beginning and then
VBA Code:
Application.ScreenUpdating = True
At the end. Let us know how it goes.
 
Upvote 0
Solution
Great. That's a relief (LOL).
Out of curiosity, are you able to give a sense of how much time it saves? It would be a useful data point in case anyone else needs to use it in future.
 
Upvote 0
I should probably have added that there is a tendency for people to forget (or not know in the first place) that screen updating has been turned off - which can become problematic when you encounter an error at some point in the code, and forget that it hasn't been turned back on. In those situations, it can mistakenly appear like Excel has stalled etc. In which case, it is best to include some error handling routines. So, as a basic example, at the outset:
VBA Code:
On Error Goto ErrHandler
Application.ScreenUpdating = False
then at the end:
VBA Code:
ErrHandler:
Application.ScreenUpdating = True
If Err.Number <> 0 Then MsgBox "Error " & Err.Number & vbNewLine & Err.Description
That way, if there if the VBA code does encounter an error, it will at least make sure to turn the ScreenUpdating property back on and will notify you/the user of the error. Just a thought.
 
Upvote 0
Thanks Dan, I like that idea and it makes the code more complete; I will add it.
Although I still feel the loading of images could go a lot faster, I will gladly post the encountered time difference in my next post.
 
Upvote 0
Come to think of it: what I'd really like toen, is to add a timer function that shows me the elapsed time between start and stop of my function...
 
Upvote 0
I've posted something like that on this forum before. Bear with me.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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