AddPictures doesn't work correctly in Excel VBA

eiloken

Board Regular
Joined
Aug 11, 2023
Messages
209
Office Version
  1. 365
  2. 2016
  3. 2003 or older
Platform
  1. Windows
Great day,
I create macro to insert picture to specific cell in each sheet in workbook, this macro will be call by parent macro which used to find specific cell in each sheet, but it seems problematic. In Sheet1 and some another sheets, it work corectly but some of them seems like set wrong "Top" property (please check attach screenshot), Can anyone help me with this problem? I preciate it.

VBA Code:
Private Sub InsertStamp(ByVal dCll As Range, ByVal stampP As String)
    Dim xSize As Double
    Call CleanDestinationRange(dCll)
    With dCll.MergeArea
        If .Width > .Height Then 'looking for smaller property of cell and set it to picture size
            xSize = .Height - 3
        Else
            xSize = .Width - 3
        End If
        'insert pictures and align it to center of width and height
        .Worksheet.Shapes.AddPicture Filename:=stampP, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
        Top:=.Top + (.Height - xSize) / 2, Left:=.Left + (.Width - xSize) / 2, Width:=xSize, Height:=xSize
    End With
End Sub
 

Attachments

  • Picture2.png
    Picture2.png
    242.4 KB · Views: 7

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
This might be a start.
 
Upvote 0
This might be a start.
yeah, i know that merge cell is inconvenience in vba but this is my company form with so many documents like that, i can not just reform all of them. beside i don't think the code has problem with merge area, it work well and like i said, some worksheet work correctly but some is not, and "Width" property of range is correct and "Top" is correct too (i tries "debug.print .Top + (.Height - xSize) / 2" to get that) but something wrong when macro move pictures and it can not move to right "Top".
 
Upvote 0
Maybe just explain what needs to happen. Be concise about it.
Oh, please don't quote me. I normally know what I wrote/suggested. We don't really need all the extra clutter.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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