Excel chart (shape) pasted as Bitmap will not adjust width in VBA? - excel

I have a chart in a spreadsheet that I want to copy as a bitmap and place somewhere else within the same sheet. My code is below, although there's not much to see. No matter what I change .width to, the width of the chart does not adjust to what I tell it to. However, the width will change if I change .Height. This indicates that the pasted Bitmap image insists on retaining its original proportions.
Is this property specific to Bitmap objects, and is there a way around this problem without changing the paste format to something other than Bitmap?
Thanks for your help.
Set AB = ActiveSheet.Shapes(ActiveSheet.Shapes.Count)
With AB
.Left = ActiveSheet.Range("AB1").Left
.Top = ActiveSheet.Range("AB1").Top
.Width = ActiveSheet.Range("AB100:BM100").Width
.Height = ActiveSheet.Range("AB1:AB50").Height
End With

I'm guessing the bitmap you pasted has "Lock Aspect Ratio" set (Format Picture -> Size & Properties). In this case, Excel will change the Width of the bitmap after you set the Height (to preserve the bitmap's aspect ratio).
Use .LockAspectRatio = msoFalse before you set the Height and Width properties.

Related

Excel 2016 and newer VBA: how to maximize a chart within/to the worksheet size?

In https://i.stack.imgur.com/s6gII.png and How to get the size (width and height) of the area with excel vsto c# excluding the ribbon area? a similar question has already been put.
Excluding all GUI elements outside/around the "inner/client" worksheet area, e.g. without the ribbon, "X/ok/fx" + cell contents, Sheet menu, "Ready + Record Macro" footer line + frame heights, left + right border + frame widths, etc.
And together with How to get the screen position of an active workbook? neither of these links has answered my needs yet. Or their instructions do not work for me, because neither of the .PageSetup.*Margin values corresponds to my worksheet "inner/client" OR "outside/GUI" borders + frames + overhead sizes.
I know, by writing
With ActiveChart.Parent 'access the chart container = the Shape
.Left = 0
.Top = 0
.Width = 4 * 72 'fixed size example
.Height = 3 * 72 'fixed size example
End With
I can set the chart position + size, in this example to a fixed size of 3 * 4 inches.
But HOW TO GET/fetch the whole currently visible dynamic worksheet "inner/client" size (at least once a method call) ?
In order to set the chart size, so that it covers/uses the whole currently visible dynamic worksheet "inner/client" size (at least once a method call). Hopefully: nothing more (no scrollbars shifting needed to see the whole chart), nothing less (no unneccessary "tiny" chart) ?
[I know, that size assignment will work for me only "for a moment" until a workbook resize happens. So I'll have to adjust the chart size again (and again (and again ...)), e.g. by using a timer (or a system timer). Or is there some "anchor/chain/nail" chart property, so that it ALWAYS keeps up with the "inner/client" worksheet size (within the workbook) ?]
Thanks for your help
This is about as good as you can do.
Sub SizeChartToWindow()
Dim VisibleRange As Range
Set VisibleRange = ActiveWindow.VisibleRange
Dim UsableRange As Range
Set UsableRange = VisibleRange.Resize(VisibleRange.Rows.Count - 1, _
VisibleRange.Columns.Count - 1)
With ActiveChart.Parent
.Left = UsableRange.Left
.Top = UsableRange.Top
.Width = UsableRange.Width
.Height = UsableRange.Height
End With
End Sub
Many thanks, that helped me a lot.
This also takes care of the worksheet scrollbars "scrolled away", wonderful.
And the right + bottom remainder of 0.00 .. 0.99 * cell sizes is "good enough" (and I have reduced the cell width to match the cell height = 20 pixels in order to reduce that remainder effect).
:)))

How to adjust column width in Excel (.Width not .ColumnWidth)

When I use the code
ActiveSheet.Range("b:Az").ColumnWidth = 5
MsgBox ActiveSheet.Range("d1").ColumnWidth
it shows 5, but when using
ActiveSheet.Range("b:Az").ColumnWidth = 5
MsgBox ActiveSheet.Range("d1").Width
it shows 30
I know that the two properties are not the same (for some reason that I can't understand), but I want actually to adjust the property .Width not .ColumnWidth for some calculations in my code, but Excel refuses to adjust .Width property and shows Error '1004' (unable to adjust with property) when I run the following code:
ActiveSheet.Range("b:Az").Width = 5
so, how to adjust .Width property?
width can not be set -see here
[https://learn.microsoft.com/en-us/office/vba/api/excel.range.width][1]
width gives you the width in pixel.
columnWidth can be set, according to ms-page: One unit of column width is equal to the width of one character in the Normal style

Resize Chart Right to Left

I need help please in resizing Excel charts to expan right versus the standard expanding left.
I input the vba code below to each chart to resize and it works fine, except for the charts of the right side of the workbook, it expands off the screen and I don't want to have to scroll right.
How can I modify the code so that it expands left versus rt?
Thanks!
Sub Chart140_Click()
With ActiveSheet.ChartObjects(Application.Caller)
If .Height = (ThisWorkbook.Sheets("configuration").Range("chrtrngzoominh")) Then
.Height = Format(ThisWorkbook.Sheets("configuration").Range("chrtrngzoomouth"))
.Width = Format(ThisWorkbook.Sheets("configuration").Range("chrtrngzoomoutw"))
Else
.Height = Format(ThisWorkbook.Sheets("configuration").Range("chrtrngzoominh"))
.Width = Format(ThisWorkbook.Sheets("configuration").Range("chrtrngzoominw"))
End If
End With
End Sub
Excel charts are positioned with the top left corner. If you want to change the size of a chart and and anchor the right hand side, you will need to use a workaround along these lines:
save the initial chart position of the top left into variables
save the initial chart width into a variable
increase the chart width
calculate the width difference between initial and the new width
adjust the top left position by the width difference

Position a shape in Excel based on its center point

Is it possible to position a shape by its center point?
I'm trying to overlay shapes onto a picture of a map in Excel and adjust the size of the shapes based on the value in a cell.
I have the sizing portion figured out, but every time my shapes size increases it slowly starts to move to the right and bottom due to the positioning of its left and top. I would like it if the positioning of my circles would not moved once the data gets refreshed. I only want the size of the shape to be altered.
Any thoughts?
If you know the X/Y (really Left/Top, since {0,0} is upper left) where you want to put it, it is as simple as subtracting off half the size of the shape to get the center to be there.
Here is some simple code which puts the center of a circle at the corner of cell E8. I assume you have some way of selecting the shape based on your question.
Sub PositionByCenter()
Dim dbl_x As Double
Dim dbl_y As Double
'select a cell just to put it somewhere
dbl_x = Range("E8").Left
dbl_y = Range("E8").Top
'grab a reference to a shape
Dim shp As Shape
Set shp = Selection.ShapeRange.Item(1)
'position by the center
shp.Top = dbl_y - shp.Height / 2
shp.Left = dbl_x - shp.Width / 2
End Sub
Here is what you get after running that with a circle selected.

Keep Chart Data Label from Wraping

I have a project where I have to put user generated data labels in a chart. These need to be on a series line as such:
![What I want format][1]
However in some instances the text wraps. How do I keep the text from wrapping in excel 10? see below
![enter image description here][2]
'-------- FORMAT DATA LABELS -------------------------------------
ActiveSheet.ChartObjects("OperBal2Takt").Activate
h = 0
For h = 2 To 7
With ActiveChart.SeriesCollection(h).Points(1)
.HasDataLabel = True
.DataLabel.Text = Cells(2018 + h, 8).Value
'.DataLabel.Width = msofit
End With
Next h
In Excel 2013, the size of your data label can be changed. In previous versions, the label cannot be resized.
In earlier Excel versions, sometimes I've stretched the chart to make it wider, but shrunk the plot area of the chart down to the original size, so the white space is wider on both sides of the chart. This works because the maximum size of a label is proportional to the chart width.
Other times I've replaced the data label with a text box, which can be flexibly resized.
use this code:
ChartObj.chart.seriescollection(1).datalabels.Format.TextFrame2.WordWrap = msoFalse
and also, but less usefull:
ChartObj.chart.seriescollection(1).datalabels.Format.TextFrame2.AutoSize = msoAutoSizeNone

Resources