Macro no longer working with new computer

sclin16

New Member
Joined
May 9, 2017
Messages
10
Hi,

I have the below macro and just got a new work comp. New comp is unable to run my macro now. I think it might be a Windows 10 issue? I'm also running Office 365. Can anyone please help? I essentially need to have a macro look up a number in column E, find the corresponding jpg in a specific folder, and populate that image in column B.

Thanks in advance for your help!
Code:
Sub Kok()
On Error Resume Next
Dim i%, ppath$, sku$


For i = 6 To 500
 sku = Cells(i, 5)
 ppath = "H:\Retail\E-Commerce\Photo Shoots" & sku & "" & sku & "_00_r17.jpg"


With ActiveSheet.Pictures.Insert(ppath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 30
.Height = 40
End With
.Left = ActiveSheet.Cells(i, 2).Left
.Top = ActiveSheet.Cells(i, 2).Top
.Placement = 1
.PrintObject = True
End With
Next
End Sub
 
Last edited by a moderator:
Good catch, Fluff. That line would suppress all errors!
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
It's the first thing I tend to look for when something "Doesn't work"
 
Upvote 0
It's the first thing I tend to look for when something "Doesn't work"
Smart - I should have done the same!
 
Upvote 0
What happens if you remove the line in red & than run the code
Code:
Sub Kok()
[COLOR=#ff0000]On Error Resume Next[/COLOR]
Dim i%, ppath$, sku$


For i = 6 To 500
 sku = Cells(i, 5)
 ppath = "H:\Retail\E-Commerce\Photo Shoots" & sku & "" & sku & "_00_r17.jpg"


With ActiveSheet.Pictures.Insert(ppath)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 30
.Height = 40
End With
.Left = ActiveSheet.Cells(i, 2).Left
.Top = ActiveSheet.Cells(i, 2).Top
.Placement = 1
.PrintObject = True
End With
Next
End Sub

I get a Run-time error '1004':
Unable to get the Insert property of the Pictures class.
 
Upvote 0
When I run it, Excel crashes and when I reopen Excel, it looks like 5 of the pictures have populated correctly. What's strange is that the macro works about 25% of the time on my new comp.
 
Upvote 0
That sounds like some of the pictures don't exist in the folder.
 
Upvote 0
How about
Code:
Sub Kok()
Dim i%, ppath$, sku$


For i = 6 To 500
   sku = Cells(i, 5)
   ppath = "H:\Retail\E-Commerce\Photo Shoots" & sku & "" & sku & "_00_r17.jpg"

   If Dir(ppath) <> "" Then
      With ActiveSheet.Pictures.Insert(ppath)
         With .ShapeRange
            .LockAspectRatio = msoTrue
            .Width = 30
            .Height = 40
         End With
         .Left = ActiveSheet.Cells(i, 2).Left
         .Top = ActiveSheet.Cells(i, 2).Top
         .Placement = 1
         .PrintObject = True
      End With
   End If
Next
End Sub
 
Upvote 0
OMG, thank you so much! It works! Just for my knowledge, what's the difference between my macro and the one that you just provided? Still a newbie at this whole thing and would love to learn from my mistakes.
 
Upvote 0
This line
Code:
If Dir(ppath) <> "" Then
Checks to see if the file exists, if it does then the picture is added to the sheet, if the picture doesn't exist the code jumps to the End If & then loops back to check the next picture.
With your code the "On Error Resume Next" just masks all & any errors which can cause further problems.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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