Macro for inserting pictures created in excel 2010 not working in excel 2013

MooseMaster05

New Member
Joined
Jun 5, 2015
Messages
7
Hello, I created a macro in excel 2010 that sizes and inserts images into a named cell based on the file name of that image. When I ran the macro in excel 2013 it runs with no errors, sizes the image appropriately but inserts the image in the incorrect location. Was there a change between these two versions of excel that would cause this? Any help would be appreciated. The code were the error must be occurring is listed below I also included my variable definitions.

Dim myPicture As String
Dim myRange As Range
Dim mySheet As Integer
Dim myPath As String
Dim StepNumber As Integer
Dim lastPicFound As Integer
Dim pagesRequired As Integer
Dim pagesToAdd As Integer
Dim pic As Shape

'begin inserting pictures
StepNumber = 1
Do While StepNumber <= lastPicFound
myPicture = myPath & "\" & CStr(StepNumber) & ".JPG"
If Dir(myPicture) <> "" Then
mySheet = StepNumber \ 12 + 1
If StepNumber Mod 12 = 0 Then
mySheet = StepNumber \ 12
End If
Set myRange = Worksheets(mySheet + 1).Range("PICTURE_" & CStr(StepNumber - (mySheet - 1) * 12))
Set p = Worksheets(mySheet + 1).Shapes.AddPicture(myPicture, False, True, myRange.Width, myRange.Height, myRange.Left, myRange.Top)
If myRange.Cells.Count = 1 Then Set myRange = myRange.MergeArea
With myRange
p.Top = .Top
p.Left = .Left
p.Width = .Height
p.LockAspectRatio = msoFalse
p.Height = .Width
With p
.Rotation = 90
.IncrementLeft p.Height / 2 - p.Width / 2
.IncrementTop p.Width / 2 - p.Height / 2
.ZOrder msoSendToBack
End With
End With
End If
StepNumber = StepNumber + 1
Loop
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Problem solved. This was a macro I have been using for a long time and am surprised that it ever worked in the first place. I changed the code to what is listed below.

'begin inserting pictures
StepNumber = 1
Do While StepNumber <= lastPicFound
myPicture = myPath & "\" & CStr(StepNumber) & ".JPG"
If Dir(myPicture) <> "" Then
mySheet = StepNumber \ 12 + 1
If StepNumber Mod 12 = 0 Then
mySheet = StepNumber \ 12
End If
Set myRange = Worksheets(mySheet + 1).Range("PICTURE_" & CStr(StepNumber - (mySheet - 1) * 12))
Set myRange = myRange.MergeArea
Set p = Worksheets(mySheet + 1).Shapes.AddPicture(myPicture, False, True, myRange.Left, myRange.Top, myRange.Width, myRange.Height)
End If
StepNumber = StepNumber + 1
Loop
 
Upvote 0
Just wanted to mention that in this case all of the cells I am using are merged cells. So there is no need for the if statement in the previous code.
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

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