+ Reply to Thread
Results 1 to 6 of 6

Centering image (horizontally) in Range

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2009
    Location
    Helmond
    MS-Off Ver
    Excel 2007
    Posts
    18

    Centering image (horizontally) in Range

    Hello,

    Excuse me if I'm asking a simple question, but I'm a bit of a noob when it comes to VBA. As a matter of fact I live on Copy and Paste and sometimes I actually understand what I've copied and I'm able to adjust it to my needs. In this manner I have made an excel file that imports pictures from my harddisk into a named Range. I have tried many other sites for an answer. I have however not been able to center the images horizontally (width of different images varies) in that Range.

    I would be very gratefull if someone could help me with this.

    Below is the code I have copied and adjusted:
    Private Sub Worksheet_Change(ByVal Target As Range)
       Application.ScreenUpdating = False
        If Target.Address = ("$B$3") Then
        Range("B4") = ""
        Range("B5") = ""
        Range("B6") = ""
        End If
        
        If Target.Address = ("$B$4") Then
        Range("B5") = ""
        Range("B6") = ""
        End If
        
        If Target.Address = ("$B$5") Then
        Range("B6") = ""
        End If
        
        If Not Intersect(Target, Range("rngDisplayName1")) Is Nothing Then
             InsertPicFromFile1 _
                strFileLoc:=Range("rngFileLocation1").Value, _
                rDestCells1:=Range("rngPicDisplayCells1"), _
                blnFitInDestHeight:=True, _
                strPicName:="MyDVPic1"
        End If
        
        If Not Intersect(Target, Range("rngDisplayName2")) Is Nothing Then
             InsertPicFromFile2 _
                strFileLoc:=Range("rngFileLocation2").Value, _
                rDestCells2:=Range("rngPicDisplayCells2"), _
                blnFitInDestHeight:=True, _
                strPicName:="MyDVPic2"
        End If
        
        If Not Intersect(Target, Range("rngDisplayName3")) Is Nothing Then
             InsertPicFromFile3 _
                strFileLoc:=Range("rngFileLocation3").Value, _
                rDestCells3:=Range("rngPicDisplayCells3"), _
                blnFitInDestHeight:=True, _
                strPicName:="MyDVPic3"
        End If
       Application.ScreenUpdating = True
    End Sub
    
    
    
    
    Sub InsertPicFromFile1( _
       strFileLoc As String, _
       rDestCells1 As Range, _
       blnFitInDestHeight As Boolean, _
       strPicName As String)
    
       Dim oNewPic As Shape
       Dim shtWS As Worksheet
    
       Set shtWS = rDestCells1.Parent
    
       On Error Resume Next
       'Delete the named picture (if it already exists)
       shtWS.Shapes(strPicName).Delete
       
       On Error Resume Next
       With rDestCells1
          'Create the new picture
          '(arbitrarily sized as a square that is the height of the rDestCells)
          Set oNewPic = shtWS.Shapes.AddPicture( _
             Filename:=strFileLoc, _
             LinkToFile:=msoFalse, _
             SaveWithDocument:=msoTrue, _
             Left:=.Left + 1, Top:=.Top + 1, Width:=.Height - 1, Height:=.Height - 1)
          
          'Maintain original aspect ratio and set to full size
          oNewPic.LockAspectRatio = msoTrue
          oNewPic.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
          oNewPic.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
          
          If blnFitInDestHeight = True Then
             'Resize the picture to fit in the destination cells
             oNewPic.Height = .Height - 1
          End If
          
          'Assign the desired name to the picture
          oNewPic.Name = strPicName
       End With 'rCellDest
    End Sub
    
    Sub InsertPicFromFile2( _
       strFileLoc As String, _
       rDestCells2 As Range, _
       blnFitInDestHeight As Boolean, _
       strPicName As String)
    
       Dim oNewPic As Shape
       Dim shtWS As Worksheet
    
       Set shtWS = rDestCells2.Parent
    
       On Error Resume Next
       'Delete the named picture (if it already exists)
       shtWS.Shapes(strPicName).Delete
       
       On Error Resume Next
       With rDestCells2
          'Create the new picture
          '(arbitrarily sized as a square that is the height of the rDestCells)
          Set oNewPic = shtWS.Shapes.AddPicture( _
             Filename:=strFileLoc, _
             LinkToFile:=msoFalse, _
             SaveWithDocument:=msoTrue, _
             Left:=.Left + 1, Top:=.Top + 1, Width:=.Height - 1, Height:=.Height - 1)
          
          'Maintain original aspect ratio and set to full size
          oNewPic.LockAspectRatio = msoTrue
          oNewPic.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
          oNewPic.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
          
          If blnFitInDestHeight = True Then
             'Resize the picture to fit in the destination cells
             oNewPic.Height = .Height - 1
          End If
          
          'Assign the desired name to the picture
          oNewPic.Name = strPicName
       End With 'rCellDest
    End Sub
    
    
    Sub InsertPicFromFile3( _
       strFileLoc As String, _
       rDestCells3 As Range, _
       blnFitInDestHeight As Boolean, _
       strPicName As String)
    
       Dim oNewPic As Shape
       Dim shtWS As Worksheet
    
       Set shtWS = rDestCells3.Parent
    
       On Error Resume Next
       'Delete the named picture (if it already exists)
       shtWS.Shapes(strPicName).Delete
       
       On Error Resume Next
       With rDestCells3
          'Create the new picture
          '(arbitrarily sized as a square that is the height of the rDestCells)
          Set oNewPic = shtWS.Shapes.AddPicture( _
             Filename:=strFileLoc, _
             LinkToFile:=msoFalse, _
             SaveWithDocument:=msoTrue, _
             Left:=.Left + 1, Top:=.Top + 1, Width:=.Height - 1, Height:=.Height - 1)
          
          'Maintain original aspect ratio and set to full size
          oNewPic.LockAspectRatio = msoTrue
          oNewPic.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
          oNewPic.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
          
          If blnFitInDestHeight = True Then
             'Resize the picture to fit in the destination cells
             oNewPic.Height = .Height - 1
          End If
          
          'Assign the desired name to the picture
          oNewPic.Name = strPicName
       End With 'rCellDest
    End Sub
    Thanks in advance...

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Centering image (horizontally) in Range

    Why do you have three InsertPicFromFile subs?

    Do you want the picture fitted to the height of the destination range and centered horizontally?

    Whay are you doing this?

    Left:=.Left + 1, Top:=.Top + 1, Width:=.Height - 1, Height:=.Height - 1
    Last edited by shg; 02-03-2011 at 01:38 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-06-2009
    Location
    Helmond
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Centering image (horizontally) in Range

    Quote Originally Posted by shg View Post
    Why do you have three InsertPicFromFile subs?
    Because I have three different pictures in three different places in the document. In the end it will be over 20 because it's going to be a fitness training program.

    Quote Originally Posted by shg View Post
    Do you want the picture fitted to the height of the destination range and centered horizontally?
    Yes!

    Quote Originally Posted by shg View Post
    Whay are you doing this?

    Left:=.Left + 1, Top:=.Top + 1, Width:=.Height - 1, Height:=.Height - 1
    This part I copied from another excel document, but I don't know how to adjust it to center the picture...

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Centering image (horizontally) in Range

    Try this:
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim i As Long
    
        If Target.Cells.Count > 1 Then Exit Sub
    
        If Not Intersect(Target, Me.Range("B3:B5")) Is Nothing Then
            Me.Range(Target, "B6").ClearContents
        End If
    
        For i = 1 To 3
            If Not Intersect(Target, Range("rngDisplayName" & i)) Is Nothing Then
                InsertPicFromFile1 _
                        sFile:=Range("rngFileLocation" & i).Value, _
                        r:=Range("rngPicDisplayCells" & i), _
                        bFitH:=True, _
                        sPic:="MyDVPic" & i
            End If
        Next i
    End Sub
    
    Sub InsertPicFromFile(sFile As String, _
                          r As Range, _
                          bFitH As Boolean, _
                          sPic As String)
        With r.Worksheet
            On Error Resume Next
            .Shapes(sPic).Delete
            On Error GoTo 0
    
            With .Pictures.Insert(Filename:=sFile)
                If bFitH Then .Height = r.Height
                .Left = r.Left + r.Width / 2 - .Width / 2
                .Top = r.Top
                .Name = sPic
            End With
        End With
    End Sub
    Last edited by shg; 02-04-2011 at 10:51 AM.

  5. #5
    Registered User
    Join Date
    06-06-2009
    Location
    Helmond
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Centering image (horizontally) in Range

    Thanks shg for your effort, but including this code introduces a whole bunch of error codes that I'm not able to eliminate....

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Centering image (horizontally) in Range

    If you're looking for help, that's not much information.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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