Inserting a picture in the correct position - excel

I'm using a code that inserts a picture (column A) of the corresponding item number located in Column B.
Current positioning of the picture:
However, the pictures that are inserted are located in the top left corner of each cell and I'd like to have them in the center of the cell a little below the cell line (cell size is 54 and picture is 50).
Here's the code that I use:
Sub InsertImageFullName()
On Error Resume Next
Application.ScreenUpdating = False
Dim path$, cl As Range, myPicture As Object
Set Rng = Range("A2:A300")
cell_h = Range("A2").Top - Range("A1").Top
For Each cl In Rng
path = cl.Offset(0, 8).Value
If path Like "*?*" Then
Set myPicture = ActiveSheet.Pictures.Insert(path)
With myPicture
.ShapeRange.LockAspectRatio = msoTrue
.Height = 50
.Top = Rows(cl.Row).Top
.Left = Columns(cl.Column).Left
End With
End If
Set myPicture = Nothing
Next
End Sub
What needs to be modified to make this work ?
Any help is greatly appreciated

To set the position of the picture you youst need to adjust the top and left position of it.
.Top = Rows(cl.Row).Top
.Left = Columns(cl.Column).Left
So you just need to add something. So if your cell height is 54 and your picture height is 50 and your picture should be centered the amount you need to add calculates like add = (CellHeight - PictureHeight) / 2 which is (54 - 50) / 2 which is 2 so you need to add 2 to the .Top position:
.Top = Rows(cl.Row).Top + 2 'add 2 to the top position of your picture.

You know image width and height by myPicture.Width and myPicture.Height. And cell width and height by cl.Width and cl.Height
Image top position is Cell top + (Cell top - Image Height) / 2
And image left position is Cell left + (Cell left - Image Width) / 2
So you need to Change your code from
.Top = Rows(cl.Row).Top
.Left = Columns(cl.Column).Left
End With
To
.Top = cl.Top + (cl.Height - myPicture.Height) / 2
.Left = cl.Left + (cl.Width - myPicture.Width) / 2
End With

Try to use Vertical Alignment and Horizontal Alignment on Range object to align content of a cell properly.

Related

Paste Pictures and Organize

I have the following code, which sorts some pictures down according to a value in Sheet1. Horizontally the images are aligned in the required columns. But not in the Rows.
I tried to do it with another FOR loop that I called J but doesn't work, it puts the images on top of each other.
How can I align the pictures in a column and a row that I choose?
What am I doing wrong? or what am I missing?
Thank you.
-------------CODE-------------
Sub CommandButton2_Click()
Dim firma_pic As Picture
Dim pic_location As String
Dim identifier_pic As String
'For j = 14 To 23
'Next
'Worksheets("Sheet3").Cells(14, 23).Select
For i = 2 To 11
identifier_pic = Worksheets("Sheet1").Cells(i, 11).Value
pic_location = "C:\Users\User\Downloads\Docs\img\" & Worksheets("Sheet1").Cells(i, 2).Value & ".png"
With Worksheets("Sheet3").Cells(i, 24)
Set firma_pic = ActiveSheet.Pictures.Insert(pic_location)
firma_pic.Top = .Top
firma_pic.Left = .Left
firma_pic.ShapeRange.LockAspectRatio = msoFalse
firma_pic.Placement = xlMoveAndSize
firma_pic.ShapeRange.Width = 70
firma_pic.ShapeRange.Height = 30
End With
Next
Worksheets("Sheet3").Cells(i, 23).Select
End Sub
Width and Height need to consider your cell too. You may resize images and force them to be W=70 and H=30 where the cell begins, but it could lead to images overlapping between columns. I'd suggest you to stick to the same workflow as you did for Top and Left
firma_pic.ShapeRange.Width = .Width
firma_pic.ShapeRange.Height = .Height

Move image to a specific location on a sheet

I have 12 pictures that I've dragged and dropped into the sheet directly from a windows folder. They are named "1 1.bmp", "1 2.bmp", "1 3.bmp" and so on.
I want to move them but how?
This is the code I'm trying:
Worksheets("R").Shapes("1 1").Top = Worksheets("R").Rows(24).Top
I don't know how reference to them. They are in the same folder as the .xlsm file. I've tried
Worksheets("R").Shapes("1 1.bmp").Top = Worksheets("R").Rows(24).Top
too.
Both examples from another question here on stack overflow.
What is the correct syntax?
/Jens
Here is the code to first insert picture to Excel and then adjust or resize the Picture. Later move the Picture towards down or right:
'Insert the Picture from the path if its not present already
Set myPict = Thisworkbook.sheets(1).Range("A1:B5").Parent.Pictures.Insert(ThisWorkbook.Path & "\" & "mypic.jpg")
'Adjust the Picture location
myPict.Top = .Top
myPict.Width = .Width
myPict.Height = .Height
myPict.Left = .Left
myPict.Placement = xlMoveAndSize
'myPict.LockAspectRatio = msoTriStateMixed
'Change the width of the Picture
myPict.Width = 85
'change the Height of the Picutre
myPict.Height = 85
End With
'Select the Picutre
myPict.Select
'Move down the picture to 3 points. Negative value move up
Selection.ShapeRange.IncrementTop 3
'Move towards right upto 5 points. Negative value moves towards right
Selection.ShapeRange.IncrementLeft 5
Try this code:
Option Explicit
Sub ArrangePictures()
Dim sh As Shape, anchor_cell As Range, v_shift As Long
With Worksheets("R")
Set anchor_cell = .Range("B24") 'left top corner for pictures
v_shift = 0 'vertical shift for next picture
For Each sh In .Shapes 'loop over all the shapes in the sheet
If sh.Type = msoPicture Then 'check if the shape is a picture
sh.Top = anchor_cell.Top + v_shift 'move picture (vertical)
sh.Left = anchor_cell.Left 'move picture (horizontal)
v_shift = v_shift + sh.Height 'add vertical shift for next picture
End If
Next
End With
End Sub

VBA excel .Left .Top when picture is rotated

I tried countlessly but I still have this problem. I try to fit a picture in to a given range with a macro in excel. As long as the picture is not rotate (orientation = 0) there's not a problem. But from the moment the picture gets rotated everything bleeds out. I have already discovered that the picture height and width are rotated as wel. But what happens to the .top and .left? Here's my code:
Function fotoInsert(ByVal PictureFileName As String, ByVal rng As Range)
Set pic = ActiveSheet.Pictures.Insert(PictureFileName)
If pic.ShapeRange.Rotation = 90 Then
With pic
'keep original aspect ratio
.ShapeRange.LockAspectRatio = msoTrue
'Picture's aspect is less than rng aspect then adjust the picture's width to fit rng
If (pic.Width \ pic.Height) <= (rng.Height \ rng.Width) Then
.Width = rng.Height - 1 'pictures' width is the larger height, by this line it fits exactly into range width
.Left = rng.Left + ((rng.Width - pic.Height) / 2)
.Top = rng.Top + 1
Else 'Picture's aspect is greater than rng aspect then adjust the picture's height to fit rng
.Width = rng.Height - 1 'picture's heigth is larger than its width, this line makes it exactly fit int range height
.Top = rng.Top
.Left = rng.Left
End If
.Placement = xlMoveAndSize
.PrintObject = True
End With
Else
With pic
'keep original aspect ratio
.ShapeRange.LockAspectRatio = msoTrue
'Picture's aspect is less than rng aspect then adjust the picture's width to fit rng
If (.Height \ .Width) <= (rng.Height \ rng.Width) Then
.Width = rng.Width - 1 'pictures' width is the larger height, by this line it fits exactly into range width
.Left = rng.Left + 1 'position at left range border
.Top = rng.Top + ((rng.Height - pic.Height) / 2) 'position in center of range height
Else 'Picture's aspect is greater than rng aspect then adjust the picture's height to fit rng
.Top = rng.Top + 1 'position at upper border of the range[/color]
.Height = rng.Height - 1 'picture's heigth is larger than its width, this line makes it exactly fit int range height
.Left = rng.Left + ((rng.Width - pic.Width) / 2) 'position in center of range width
End If
.Placement = xlMoveAndSize
.PrintObject = True 'make sure picture gets printed
End With
End If
End Function
So the problem occurs in the very first If condition (rotation = 90)
In the other cases I don't seem to have a problem.
A typical range I use to test is:
"A7:N46"
Thanks for your fast response!
What I try to do is to fit any given picture in a pre determined range. So yes, I also will receive rotated pictures.
The idea is that the user selects pictures (up to 4) and automatically they are arranged and resized to fit in one pre-determined excel sheet. So I made 4 scenario's (with different ranges) how to arrange those pictures on one sheet. This is done in an other routine via a switch case.
So far this code works with non rotated pictures. From the moment there's a rotation in there it is getting messy and pictures 'bleed out'. If there's an other way to tackle my problem, I'm glad to here it.
I had exactly the same problem as described here. It is unclear what happens with top and left properties of the shaperange when rotation 90 or 270 occurs.
In case of rotation, my workaround was:
create a new hidden sheet;
insert the rotated picture;
hard code the position of the picture;
copy the rows to the range where you want it;
delete the temporary sheet.

VBA scroll to text (search result)

I have a function which searchs through all the textboxes in a file and when it finds the text it automatically scrolls the window to the shape that contains the text.
Is it possible to:
a) scroll to the text, instead of the shape (scroll the page and center it on the search text result)
b) if not possible, then scroll to the middle of the shape (now it's going to the top)
If InStr(LCase(sTemp), LCase(sFind)) <> 0 Then
shp.Select
With shp.Line
.ForeColor.RGB = vbRed
.Weight = 5
End With
sFind2 = LCase(sFind)
sTemp2 = LCase(shp.TextFrame.Characters.Text)
iPos2 = InStr(sTemp2, sFind2)
If iPos2 > 0 Then
With shp.TextFrame.Characters(Start:=iPos2, _
Length:=Len(sFind2)).Font
.Size = 35
End With
End If
shp.Select
ActiveWindow.ScrollRow = shp.TopLeftCell.Row
ActiveWindow.ScrollColumn = shp.TopLeftCell.Column
End If
Useless to say: I am not an expert, sorry for the bad formatting.
a) you cannot scroll to a text. You can just scroll full rows and full columns.
b) you can do that only approximately, because you can just scroll full rows and full columns.
Since shp.TopLeftCell finds the top left cell under the shape, you can try to use the height/width of the shape and cells to find the cell that is approximately in the middle of the shape.
'scroll additional rows
Dim CellHeights As Double
CellHeights = shp.TopLeftCell.Height
Dim iRow As Long
Do While CellHeights < shp.Height / 2
CellHeights = CellHeights + shp.TopLeftCell.Offset(RowOffset:=iRow).Height
iRow = iRow + 1
Loop
ActiveWindow.ScrollRow = shp.TopLeftCell.Offset(RowOffset:=iRow).Row
'scroll additional columns
Dim CellWidths As Double
CellWidths = shp.TopLeftCell.Width
Dim iCol As Long
Do While CellWidths < shp.Width / 2
CellWidths = CellWidths + shp.TopLeftCell.Offset(ColumnOffset:=iCol).Width
iCol = iCol + 1
Loop
ActiveWindow.ScrollColumn = shp.TopLeftCell.Offset(ColumnOffset:=iCol).Column

Scaling pictures using Excel VBA

I’m trying to scale pictures to fit on a cell of height 172.75.
If sPhoto > -1 Then
x.RowHeight = AltRow + x.Font.Size + 2
On Error GoTo IsError
factor = CSng(AltRow / Selection.ShapeRange.Height)
If factor > CSng(x.Width / Selection.ShapeRange.Width) Then
factor = CSng(x.Width / Selection.ShapeRange.Width)
End If
If factor < 0.5 Then
factor = factor / 3.8
End If
With Selection
.ShapeRange.LockAspectRatio = msoTrue
.ShapeRange.ScaleWidth factor, msoTrue, msoScaleFromTopLeft
.ShapeRange.ScaleHeight factor, msoTrue, msoScaleFromTopLeft
.ShapeRange.Top = x.Top
.ShapeRange.Left = x.Left
End With
End If
Some pictures have a really big size in terms of height and width in their original format. I need the scale factor to more flexible.
Found a way to scale the pictures to snap to the bounds of the cell.
Dim AspectRatio As Double
Dim W, H As DoubleI
if SketchPhoto > -1 Then
x.RowHeight = AltRow + x.Font.Size + 2 'Adjusting height to fit the picture for each cell
With Selection.ShapeRange
.LockAspectRatio = msoTrue
AspectRatio = .Width / .Height
.Left = x.Left
.Top = x.Top
W = x.Width ' width of cell range
H = x.Height ' height of cell range
If (W / H < AspectRatio) Then
.Width = W - x.Font.Size + 0.5 ' scale picture to available width
Else
.Height = H - x.Font.Size + 0.5 ' scale picture to available height
End If
Range("A1").Activate
End With
End If

Resources