Can anyone suggest a way to make this macro run mach fast as it take a very long time to run?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I was woundering if anyone know a quicker way to insert images from urls into a sheet,
this currently take about two hours to run on 5000 lines

any suggestions would be greatly apreciated.
hears the code i currently use?

VBA Code:
Sub Hyperlink_Insert()

    Dim Pshp As Shape
    Dim xRg As Range
    Dim xCol As Long
    On Error Resume Next
    Application.ScreenUpdating = False
    
    Dim shp As Shape

For Each shp In ActiveSheet.Shapes
    If Left(shp.Name, 7) = "Picture" Then
    shp.Delete
    End If
Next shp


lr1 = Thum.Cells(Rows.Count, "B").End(xlUp).Row
If lr1 <= 10 Then
lr1 = 10
End If

    
    Set rng = Thum.Range("B10:B" & lr1)
    For Each cell In rng
    Application.StatusBar = "Progress: " & cell.Row & " of " & lr1
    
        filenam = cell
        Thum.Pictures.Insert(filenam).Select
        Set Pshp = Selection.ShapeRange.Item(1)
        If Pshp Is Nothing Then GoTo lab
        xCol = cell.Column
        Set xRg = Cells(cell.Row, xCol)
        With Pshp
            .LockAspectRatio = msoTrue

            .Height = xRg.Height * 0.9
            .Top = xRg.Top + (xRg.Height - .Height) / 2
            .Left = xRg.Left + (xRg.Width - .Width) / 2
'            .OnAction = "Picture_Click_Big"
        End With
lab:
    Set Pshp = Nothing
    Range("A1").Select
    Next
    Application.ScreenUpdating = True



Application.StatusBar = False
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Without having a file to test on, I can recommend a few things:

1) Get rid of the 'On Error Resume Next' line of code. That will only hide errors that you run into, it should never be placed at the beginning of your code and left in tact for the entire procedure.
2) Limit or get rid of entirely the 'Application.StatusBar = "Progress: " & cell.Row & " of " & lr1' line of code. Updating the StatusBar slows your script every time you do it.
3) Change:
VBA Code:
            Set Pshp = Nothing
            Range("A1").Select
        Next

to:
VBA Code:
        Next
'
        Set Pshp = Nothing
        Range("A1").Select
 
Upvote 0
wow, that made a noticeable difference, thanks JohnyL,
it still takes some time to run but I'd say half as long as it did,
if anyone else has anything to either complement the above or suggest a better way of doing this I'd be happy to hear.
Thanks
Tony
 
Upvote 0
In that case, you possibly could benefit from changing:
VBA Code:
        Application.ScreenUpdating = False

to:
VBA Code:
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual


Also change:
VBA Code:
        Application.ScreenUpdating = True

to:
VBA Code:
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

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