+ Reply to Thread
Results 1 to 5 of 5

Centering a picture in a merged cell using VBA

  1. #1
    Registered User
    Join Date
    02-23-2023
    Location
    Canada
    MS-Off Ver
    365
    Posts
    7

    Centering a picture in a merged cell using VBA

    Hello,

    I am new to using VBA, but thanks to forums like these I have had success in achieving most of what I set out to accomplish with this specific workbook.

    One thing I am hung up on is finding a solution to properly centering a picture in an area of merged cells once it has been inserted via the same macro.

    Here's a screenshot of what I am attempting to achieve:

    Screenshot_20230223_090747.png

    Here's a screenshot of what happens when I run the macro below:

    Screenshot 2023-02-23 212231.jpg

    As you can see, I have a merged cell with the range A12:AZ31. I have attempted about eight different solutions that apparently worked for others on various forums, however there must be something unique about my case as none of them seem to quite work for me. Here's what I've got as an example, but again this does not center the image properly either:

    Sub AddOverviewPhoto()

    ' AddOverviewPhoto Macro
    ' Adds and resizes the main report photo.

    ActiveSheet.Unprotect

    Dim photoNameAndPath As Variant
    Dim photo As Picture
    Dim r As Range

    photoNameAndPath = Application.GetOpenFilename(Title:="Select Photo to Insert")
    If photoNameAndPath = False Then Exit Sub
    Set r = ActiveSheet.Range("A12:AZ31")
    Set photo = ActiveSheet.Pictures.Insert(photoNameAndPath)
    With photo
    .Left = r.Left + (r.Width - .Width) / 2
    .Top = r.Top + (r.Height - .Height) / 2
    .Height = r.Height * 0.98
    End With

    ActiveSheet.Protect

    End Sub


    I've read about and tried to incorporate .MergeArea where some forum posts had suggested, but I haven't seemed to get anywhere with that either.

    The picture insertion and resizing work perfectly fine, just need to get the darn thing centered in that range of merged cells.

    Any and all suggestions are greatly appreciated!

    Thanks,

    Matt

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Centering a picture in a merged cell using VBA

    try this

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    02-23-2023
    Location
    Canada
    MS-Off Ver
    365
    Posts
    7

    Re: Centering a picture in a merged cell using VBA

    This worked perfectly Andy, thank you!

    I am still not certain why my earlier code was not working, but perhaps as my understanding of VBA increases it will come to me!

    Thanks again.
    - Matt

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Centering a picture in a merged cell using VBA

    if you step through the code line by line you will see how the image moves around and ends up not centered.

    depending on the size of the image the left position may not work as intended if the image is too wide.
    Whilst the formula will produce a value for Left to center the image if the values is negative, which is off the worksheet, it will be capped at zero.
    Same for height if it goes negative.

    Then with the image centered, as best as possible, you adjust the height. As the image probably loads with aspect ratio locked this will alter the width.

  5. #5
    Registered User
    Join Date
    02-23-2023
    Location
    Canada
    MS-Off Ver
    365
    Posts
    7

    Re: Centering a picture in a merged cell using VBA

    That makes sense. Thanks again Andy.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Picture insert from directoy in merged cell and scaling
    By heinzpol in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 10-20-2022, 04:53 AM
  2. Centering Image in Merged Range
    By saq7792 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-15-2019, 07:10 PM
  3. Resize picture to fit merged cell
    By TitansGo in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-29-2016, 04:20 PM
  4. Need to embed picture into merged cell bloc.
    By Ochimus in forum Excel General
    Replies: 0
    Last Post: 03-10-2015, 01:16 PM
  5. [SOLVED] import picture to fit in merged cell
    By alexshaw76 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-01-2014, 09:35 PM
  6. Import picture to fit in merged cell
    By alexshaw76 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2014, 03:37 PM
  7. Auto Resize Pasted Picture to merged cell
    By CyberPath in forum Excel General
    Replies: 2
    Last Post: 04-15-2012, 07:13 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1