Resize Chart Right to Left - excel

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

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).
:)))

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

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.

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

Excel: How can I add custom gridline to a chart?

How can I add a custom horizontal line that has a label and it is at the exact same level as the first column in the chart (see the screenshot below).
Can this be done in VBA?
This could be done in VBA, or it could be done without VBA:
http://peltiertech.com/Excel/Charts/AddLineHorzSeries.html
This method involves creating a secondary Y-axis, and plotting another series of data in a "line" on the second axis.
This is a fairly clean solution.
Otherwise with VBA you would need add a shape/line to the chart (important to add it to the chartObject and not to the Worksheet).
Then compute the height of points and make the line's .Left = the chart's .PlotArea.Left and make the line's .Width = to the chart's .PlotArea.Width. Then set the line's .Top value based on the chart's .PlotArea.Height minus the "height" you calculated for the point.
using vba, you can add a new series:
With ActiveChart.SeriesCollection.NewSeries
.Values = "={6.9,6.9,6.9,6.9}"
'create string beforehand if number and values are unknown
.ChartType = xlLine
'and whatever other formatting is needed
End With
not using VBA, you can add a new column to the data, and put all of it equal to the first item, using =$B$2 in each cell to add the line to the graph

Resources