Aligning imported images in cell

excelbeginner1

New Member
Joined
Dec 13, 2014
Messages
20
Good day
Please could you assist I need to align my imported images

this is the vba code I am currently using to import.
Sub PictureKiller()
Dim s As Shape, rng As Range
Set rng = Range("c6:c99999")

For Each s In ActiveSheet.Shapes
If Intersect(rng, s.TopLeftCell) Is Nothing Then
Else
s.Delete
End If
Next s



Const fPath = "H:\FURNITURE PICS\ALL PICS"
Dim cel As Range, picPath As String


For Each cel In Range("a6", Range("a" & Rows.Count).End(xlUp))
On Error Resume Next
picPath = fPath & "\" & cel.Value & ".jpg"
If Not Dir(picPath, vbDirectory) = vbNullString Then
With cel.Parent.Pictures.Insert(picPath)
With .ShapeRange
.LockAspectRatio = msoFalse
.Width = 310
.Height = 310
End With
.Left = cel.Offset(, 2).Left
.Top = cel.Offset(, 2).Top
End With
End If
Next cel
End Sub

currently the images are are the left of the cell.
I want to center the images in there respective cells,.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try replacing your .Left = cel.Offset(, 2).Left with
VBA Code:
.Left = cel.Offset(, 2).Left + (cel.Offset(, 2).Width - .Width) / 2


PS: None of your questions has been marked as "Solved", may I suggest that you have a look at Guidelines and Mark as Solution
 
Last edited:
Upvote 0
Solution
Try replacing your .Left = cel.Offset(, 2).Left with
VBA Code:
.Left = cel.Offset(, 2).Left + (cel.Offset(, 2).Width - .Width) / 2


PS: None of your questions has been marked as "Solved", may I suggest that you have a look at Guidelines and Mark as Solution
Thanks this helps , how would I move the picture down, I tried .top = cel.Offset(, 2).top + (cel.Offset(, 2).height - .height) / 2, but this centers the images
 
Upvote 0
You set the top position to the top of the row by .Top = cel.Offset(, 2).Top
Which vertical position would you like to use instead of that one?
 
Upvote 0
Thanks , I managed to figure it out.

.Left = cel.Offset(, 2).Left + (cel.Offset(, 2).Width - .Width) / 2
.Top = cel.Offset(, 2).Top + (cel.Offset(, 1).Height - .Height) / 2
 
Upvote 0
The answer has been provided in post #2. Therefore, the marked solution post has been changed accordingly to help future readers.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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