+ Reply to Thread
Results 1 to 10 of 10

Centering Image in Merged Range

Hybrid View

  1. #1
    Registered User
    Join Date
    04-03-2015
    Location
    NY NY
    MS-Off Ver
    Office 365
    Posts
    46

    Centering Image in Merged Range

    My end goal is to center an image in a merged range. I found code that will allow me to center an image in a cell but I cant seem to figure out how to (or even if its possible to) modify it to work on a merged range instead of a single cell.

    If anyone has any idea about how to modify this code, or a different way all together to go about this any help would be much appreciated!

    In the attached file ctrl+w will center an object in the selected cell provided that the top right corner of the object is located in that cell.
    Attached Files Attached Files

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Centering Image in Merged Range

    Hi there,

    Take a look at the attached version of your workbook and see if it does what you need. It uses the following code:

    
    
    
    Option Private Module
    Option Explicit
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub CheckShapeAndRange()
    
        Dim rTargetRange    As Range
        Dim bShapeFound     As Boolean
        Dim rCell           As Range
        Dim shp             As Shape
    
        If TypeName(Selection) = "Range" Then
    
              Set rTargetRange = Selection
    
              For Each shp In ActiveSheet.Shapes
    
                  bShapeFound = False
    
                  For Each rCell In rTargetRange.Cells
    
                      If rCell.Address = shp.TopLeftCell.Address Then
                          bShapeFound = True
                          Exit For
                      End If
    
                  Next rCell
    
                  If bShapeFound = True Then
    
                        Call CentreShape(rTargetRange:=rTargetRange, shp:=shp)
                        Exit For
    
                  Else: MsgBox "The selected cells do not contain a Shape", vbExclamation
                        Exit For
    
                  End If
    
              Next shp
    
        Else: MsgBox "Select a range before using this feature", vbExclamation
    
        End If
    
    End Sub
    
    
    '=========================================================================================
    '=========================================================================================
    
    
    Sub CentreShape(rTargetRange As Range, shp As Shape)
    
        Dim dRangeBottom    As Double
        Dim dRangeRight     As Double
        Dim rBottomCell     As Range
        Dim rRightCell      As Range
        Dim dShapeLeft      As Double
        Dim dRangeLeft      As Double
        Dim dRangeTop       As Double
        Dim dShapeTop       As Double
    
        With rTargetRange
    
            dRangeTop = .Cells(1, 1).Top
    
            Set rBottomCell = .Cells(.Cells.Count)
            dRangeBottom = rBottomCell.Top + rBottomCell.Height
    
            dRangeLeft = .Cells(1, 1).Left
    
            Set rRightCell = .Cells(.Cells.Count)
            dRangeRight = rRightCell.Left + rRightCell.Width
    
            If (shp.Width <= (dRangeRight - dRangeLeft)) And _
               (shp.Height <= (dRangeBottom - dRangeTop)) Then
    
                  dShapeTop = dRangeTop + ((dRangeBottom - dRangeTop - shp.Height) / 2)
    
                  dShapeLeft = dRangeLeft + ((dRangeRight - dRangeLeft - shp.Width) / 2)
    
                  shp.Top = dShapeTop
                  shp.Left = dShapeLeft
    
            Else: MsgBox "The Shape cannot be accommodated within the selected range", _
                          vbExclamation
    
            End If
    
        End With
    
    End Sub

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    04-03-2015
    Location
    NY NY
    MS-Off Ver
    Office 365
    Posts
    46

    Re: Centering Image in Merged Range

    Thanks Greg, this is exactly what I was trying to accomplish, I just didn't have enough VBA experience to code it! But looking at your code, it makes total sense.

  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 Image in Merged Range

    I have replaced ActiveCell with a object variable use to keep the code short. And it now uses the merge area of the activecell


    Sub CenterShpIfInActiveCell()
    'https://www.mrexcel.com/forum/excel-questions/222400-center-graphic-excel-cell.html
        
    'If the Top-Left corner of any shape is located within the Active Cell
    'Then center the shape within the Active Cell
     
        'Dim Shp As Picture
        Dim Shp As Shape    'Modified to handle a shape
        Dim ac As Range
        
        'For Each Shp In ActiveSheet.Pictures
        Set ac = ActiveCell.MergeArea
        For Each Shp In ActiveSheet.Shapes 'Modified for a shape
        
            If inDebug Then MsgBox Shp.Name
        
            If isInBetween(ac.Left - 1, ac.Left + ac.Width, Shp.Left) And _
               isInBetween(ac.Top - 1, ac.Top + ac.Height, Shp.Top) _
               Then
                    Shp.Left = ac.Left + ((ac.Width - Shp.Width) / 2)
                    Shp.Top = ac.Top + ((ac.Height - Shp.Height) / 2)
            End If
            
        Next Shp
           
    End Sub
    Cheers
    Andy
    www.andypope.info

  5. #5
    Registered User
    Join Date
    04-03-2015
    Location
    NY NY
    MS-Off Ver
    Office 365
    Posts
    46

    Re: Centering Image in Merged Range

    Spoke a bit too soon - if I want to assign this to a hotkey instead of a button, how would I go about doing that?

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Centering Image in Merged Range

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    You're welcome - glad I was able to help.


    To assign a hotkey:

    go into the VBA CodeModule and delete or comment-out the "Option Private Module" statement;

    go back into Excel and use Alt+F8 to display the Macro menu;

    select the "CheckShapeAndRange" macro and then click on the "Options . . ." button;

    on the "Macro Options" menu, enter whichever key you wish to use, and then click on "OK".


    Hope this helps.


    Best regards,

    Greg M



    P.S.


    But looking at your code, it makes total sense

    Thank you - that's a very nice compliment to receive.
    Last edited by Greg M; 10-15-2019 at 02:41 PM. Reason: P.S. added

  7. #7
    Registered User
    Join Date
    04-03-2015
    Location
    NY NY
    MS-Off Ver
    Office 365
    Posts
    46

    Re: Centering Image in Merged Range

    AHHHH - I was wondering why it wasn't showing up in the macro menu. Removed that and it popped right up. I did a quick Google search and understand the basic use of that command but im curious why you included it in the first place?

    Also, it only seems to be working on the first range I use it on. I get an error saying there is no shape in the range if I try to execute it on a second range. In the attached it works as expected in range 1, but in 2 and 3 it insists there isn't a shape there. Any thoughts?

    Thanks,
    Simon
    Attached Files Attached Files

  8. #8
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Centering Image in Merged Range

    Hi again Simon,

    Dang it all! That's what happens when you test your code on a "nice little workbook" instead of on a "real" one!

    The attached workbook contains a new version of the "CheckShapeAndRange" routine (shown below), and appears to do what you need.

    
    
    Sub CheckShapeAndRange()
    
        Dim rTargetRange    As Range
        Dim bShapeFound     As Boolean
        Dim rCell           As Range
        Dim shp             As Shape
    
        If TypeName(Selection) = "Range" Then
    
              Set rTargetRange = Selection
    
              For Each shp In ActiveSheet.Shapes
    
                  bShapeFound = False
    
                  For Each rCell In rTargetRange.Cells
    
                      If rCell.Address = shp.TopLeftCell.Address Then
                          bShapeFound = True
                          Exit For
                      End If
    
                  Next rCell
    
                  If bShapeFound = True Then
    
                        Call CentreShape(rTargetRange:=rTargetRange, shp:=shp)
                        Exit For
    
                  End If
    
              Next shp
    
              If bShapeFound = False Then
                  MsgBox "The selected cells do not contain a Shape", vbExclamation
              End If
    
        Else: MsgBox "Select a range before using this feature", vbExclamation
    
        End If
    
    End Sub

    Regarding:


    but I'm curious why you included it in the first place?

    I normally include the "Option Private Module" statement to prevent macros being made available to "curious Users" via the Macro menu - I prefer to make macros available via CommandButtons on a worksheet or icons on the Ribbon.


    Hope the above helps - as always, please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-03-2015
    Location
    NY NY
    MS-Off Ver
    Office 365
    Posts
    46

    Re: Centering Image in Merged Range

    That did it! Thanks again for all your help. And for the explanation - makes sense in terms of keeping prying hands off your code!

    Best,
    Simon

  10. #10
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Centering Image in Merged Range

    You're welcome - glad to have helped out.

    Feel free to shout if there's anything else you need.

    Regards,

    Greg M

+ 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. [SOLVED] Clear merged and non-merged cells in named range.
    By IMM Tech in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2018, 12:37 PM
  2. Centre image (horizontally) across merged range
    By Fialko in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-16-2017, 02:22 AM
  3. Replies: 1
    Last Post: 10-22-2016, 04:27 AM
  4. [SOLVED] Pasting an image into selected merged cells. VBA to size automatically to selected range
    By DCC_PD in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-28-2015, 09:57 AM
  5. automatically fit an image into merged cells for full width of merged cells
    By Marcin4111 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-18-2014, 03:12 PM
  6. automatically fit an image into merged cells
    By dave in forum Excel General
    Replies: 3
    Last Post: 11-18-2014, 06:26 AM
  7. Centering image (horizontally) in Range
    By speedone in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-04-2011, 10:51 AM

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