Speeding up image inserts [vba]

DataBlake

Well-known Member
Joined
Jan 26, 2015
Messages
781
Office Version
  1. 2016
Platform
  1. Windows
Code:
Sub URLPictureInsert()
    Dim Pshp As Shape
    Dim i As Long
    Dim lastRow As Long
    Dim urlRng As Range
    Dim trgtRng As Range
    
    
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow

   Set urlRng = Range("B" & i)
   Set trgtRng = Range("C" & i)

If urlRng = "" Then Range("H" & i).Value = 0
If urlRng = "" Then GoTo lastline
    
        filenam = urlRng
        On Error Resume Next
        ActiveSheet.Pictures.Insert(filenam).Select
        On Error Resume Next
        Set Pshp = Selection.ShapeRange.Item(1)
        If Pshp Is Nothing Then GoTo lab
        Range("H" & i).Value = 1
    With Pshp
            .LockAspectRatio = msoFalse
            .Width = 15
            .Height = 15
            .Top = trgtRng.Top
            .Left = trgtRng.Left
    End With
lab:
    If Pshp Is Nothing Then Range("H" & i).Value = 0
    Set Pshp = Nothing
lastline:
    Next i
End Sub

is there anything i can do to this code that will make it go faster?
i understand that deleting the ".select" would make it faster, but i'm unsure of what the alternative is?
and how much time would screen updating = false save?
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
i understand that deleting the ".select" would make it faster, but i'm unsure of what the alternative is?

Try replacing...

Code:
        On Error Resume Next
        ActiveSheet.Pictures.Insert(filenam).Select
        On Error Resume Next
        Set Pshp = Selection.ShapeRange.Item(1)

with

Code:
        On Error Resume Next
        Set Pshp = ActiveSheet.Pictures.Insert(filenam)
        On Error GoTo 0

and how much time would screen updating = false save?

I don't know, but it should help.
 
Upvote 0
I don't know, but it should help.

I will try this out on monday and let you know the results. currently it is running at 3-6 seconds on average per line.
so about an hour and 15 minutes per 1000 lines
Will have an update with new times after the weekend.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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