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.
Related
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
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
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.
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
I have created a grid of text boxes, but I cannot figure out a method to change the font size and alignment (centralise vertically and horizonatally) of the text inside a named textbox.
Sub addtxtbx()
Dim shp As Shape
Dim i As Integer, j As Integer, k As Integer
Dim cindx as long, rindx as long
For i = 1 To 145
Set shp = ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, _
100 + cindx, 100 + rindx, 50, 50)
cindx = (i - Int((i - 1) / 4) * 4 - 1) * 50 + Int((i - 1) / 48) * 200
rindx = (Int((i - 1) / 4) - Int((i - 1) / 48) * 12) * 50
With shp
.Name = "TxtBx" & (i - 1)
.Fill.ForeColor.RGB = RGB(204, 102, 255)
End With
Next i
With ActiveSheet.Shapes.Range(Array("TxtBx11")).TextFrame2.TextRange
.Characters.Text = "R"
End With
End Sub
Also, I have been scratching my head with the For Next loop's i counter. I used it to rename the text boxes and found that despite using i = 1 to 144, TxtBx144 is not the last box, but the second last, TxtBx2 is the first. So, I tried to get around it by using 1 to 145 and name the box using (i-1) serial which is a bit of a cop out. What did I do wrong that I am failing to see?
What you want to do is this:
shp.ShapeRange.TextFrame2.TextRange.Font.Size = 20
where 20 is whatever size you want.
I don't know why your text boxes were getting numbered incorrectly, but your code as written is actually giving me 0-143, not 1-144 so I suspect there is either some code changing i that you haven't put into the question or something else is going on.
Also, the easiest way to figure out how to code stuff like this, is to record a macro, do whatever it is you are trying to accomplish, stop the macro, and then look at the code.