Insert picture using VBA produces two pictures

bhooper

New Member
Joined
Nov 22, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm using the following VBA to insert a photo into a cell in Excel. The code runs, but inserts the photo twice, every time. I've debugged it by stepping thru one line at a time and it definitely loops thru the code twice before ending.

I've tried a couple of different methods to insert the photo and every method does the same thing. What am I missing?

Sheets("Sheet2").Select
ActiveSheet.Shapes("lightningbolt").Select
Selection.Copy
Sheets("Sheet1").Select
ActiveSheet.Range("$e$4").Select
ActiveSheet.Paste
ActiveSheet.Range("$A$1").Select

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
That parte of your macro doesn't insert photos twice. Look somewhere else for the cause of the issue.
Is the code part of a bigger macro ? How are you triggering the macro ? maybe the code is part of an event macro like WorkSheet_Change ?
 
Upvote 0
Ah, yes. there is some other things going on above this, and your comment about the Worksheet_Change did trigger a possibility. This code is part of the Worksheet_Change subroutine.
The code above is re-triggering the change event. I was able to confirm that. I couldn't see it until I added the code to insert the photo.

The code above is:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$C$35" Then
If Range("$C$35").Value = "" Then
Range("$C$36").Value = ""
Else: Range("$C$36").Value = "Please Select..."
End If
End If

This changes the contents $C$36 based on the selection of a dropdown in $C$35. I'm assuming that the Else: Range... code is triggering the event again. Correct?
 
Upvote 0
So, I added the lines to disable and enable the events during the subroutine and that fixed it.

Thanks
 
Upvote 0
Solution
Ok, I was writing my post just to say that.
Changes to cell C36 trigger the event macro.
Glad having been of some help(y).
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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