Select image and post into merged cell

captzero

New Member
Joined
Aug 24, 2015
Messages
10
Hi All,
I'm struggling to place an image in a merged cell in multiple worksheets.
My command button needs to open the folder, allow the user to select an image file, and resize it and centre it to fit in a merged cell in 4 spreadsheets.
The merged cells are the same size and in the same location in the 4 spreadsheets (G8:H16).
image_2022-10-27_061559159.png
The code I have nearly gets it right but I can't resize and centre it in the merged cell as I'd like.

Thank you in advance.

VBA Code:
'Inserts job image into Pricing, Quote, Invoice and Receipt spreadsheets.
Private Sub CommandButton1_Click()
Dim fNameAndPath As Variant
Dim img As Picture
Dim SheetsNames(), i As Long

SheetsNames = Array("Project Pricing Calculator", "Customer Job Quote", "Customer Invoice", "Customer Receipt")
fNameAndPath = Application.GetOpenFilename(Title:="Locate the Job Image File To Be Imported")

If fNameAndPath = False Then Exit Sub
 
    For i = LBound(SheetsNames) To UBound(SheetsNames)
        Set img = ActiveWorkbook.Sheets(SheetsNames(i)).Pictures.Insert(fNameAndPath)
        With img
           'Resize Picture to fit in the cell range....
           .ShapeRange.LockAspectRatio = msoTrue              ' lock aspect ratio checkbox not selected
           '.Left = ActiveSheet.Range("G8:H16").Left
           '.Top = ActiveSheet.Range("G8:H16").Top
           .Placement = 1
           .PrintObject = True
        End With
    Next i
    Worksheets("Project Pricing Calculator").Activate
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim fNameAndPath As Variant
  Dim img As Picture
  Dim SheetsNames(), i As Long
  Dim rng As Range
  Dim sh As Worksheet
  
  SheetsNames = Array("Project Pricing Calculator", "Customer Job Quote", _
                      "Customer Invoice", "Customer Receipt")
  fNameAndPath = Application.GetOpenFilename(Title:="Locate the Job Image File To Be Imported")
  
  If fNameAndPath = False Then Exit Sub
  For i = LBound(SheetsNames) To UBound(SheetsNames)
    Set sh = Sheets(SheetsNames(i))
    'Delete the old image to put the new image
    On Error Resume Next: sh.Pictures("NewPicture").Delete: On Error GoTo 0
    Set rng = sh.Range("G8:H16")
    Set img = sh.Pictures.Insert(fNameAndPath)
    With img
      .Name = "NewPicture"
      'Resize Picture to fit in the cell range....
      .ShapeRange.LockAspectRatio = msoFalse    ' lock aspect ratio checkbox not selected
      .Left = rng.Left
      .Top = rng.Top
      .Width = rng.Width
      .Height = rng.Height
      .Placement = 1
      .PrintObject = True
    End With
  Next i
  Sheets(SheetsNames(0)).Activate
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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