How can i export a chart and multiple rows from an excel sheet to pdf - vba

I have an organogram chart in excel and few rows based on which this organogram chart is constructed. Both the chart and data rows are in the same worksheet.
I need to get this chart printed on the upper half of the pdf and the data rows just below the chart.
I have figured out how to get both chart and data rows printed individually in the pdf but i am unable to figure out how to get the data rows printed below the chart on the same page.

Select everything you want to be exported. Select Right Click / Set Print Area. This way you should be able to export what you have selected explicitly.
If you have difficulty printing everything on one page, play around with Scaling properties on the Print Preview page and try exporting again.

Related

Create a VBA Macro to create BoxPlot Charts

I am a beginner in VBA, so be indulgent in my lack of methodology while working on some VBA macro for Excel.
My goal is to create some BoxPlot Charts in Excel at this time, I have been able to create some For/If loop to capture the Data in my different worksheets (ex: I have a Ftotal column in each worksheet, but from different length that I want to add to my boxplot). For some graphs (xlcolumnclustered, ...) I have been able to simply write:
ActiveChart.ChartType = xlColumnClustered
But, when I want to do it for a box plot (xlboxwhiskler) I have not been able to complete it. So I have tried to record a macro while I was creating a box plot graphs in a chart sheet and I have been able to capture:
ActiveSheet.Shapes.AddChart2(406, xlBoxwhisker).Select
Which is not working when I am runing my Macro. Btw way, I can't figure our why I get an Activesheet instruction whereas I was on a chart page (seems strange to me, I was expecting an ActiveChart).
So my question are:
Is there a xlboxwhiskler working to create a Box Plot Chart ?
If not, is there another way ? (I have some info from here and here). Just FMY, why is Set used for ?
Finally my last solution is about to calculate everything (it might be challenging to do so), but the link here might be useful
If you have any suggestion, I remain open for it. It will then post my solution.

Flexible Data Label Formatting

I'm hoping to add custom data labels to a stacked bar chart in Excel 2010. Each bar will have a label value pulled from a value in the sheet which is not necessarily equal to the height of the bar itself.
The sheet I'm making is a template and has drop-down selections to change the units of the data. For example: $MM with 1 decimal point, K with 2 decimal points, $ with 0 decimal points, etc... I'm running into an issue formatting the labels to match the data formatting selections. I've tried two approaches:
Formatting the data labels themselves using VBA. Partial code below for $K with one decimal:
`With ActiveChart.SeriesCollection(x).Points(y)`
.DataLabel.Text.NumberFormat = "$#,##0.0,"
Unfortunately, the labels don't seem to read the commas and divide by one thousand, so I can't accurate display thousands or millions. A value of 1000 in the above code will display as $1,000.0 instead of the desired $1.0.
Format the values in the worksheet, and then just read them with VBA. Basically, I use conditional formatting to properly display the values on the sheet, and then read them with .DataLabel.Text = Cells(r,c)
Unfortunately, though the data is formatted correctly, the values are unchanged and the labels display with no custom formatting at all.
Is there any reasonable way to achieve my goal? I'm hoping there's a way to either (1) make commas count when changing data label number formats or (2) change the actual values of the data in my sheet based on the formatting drop-down selections (instead of just giving them custom formatting which leaves the value unchanged).
Thank you,
Lance
I think the words "conditional formatting" are your issue here: I've had problems with VBA reading the underlying format of a cell when it's conditionally formatted (admittedly, my issue was with colour, rather than number format, but I expect the reasons for it not working would be the same).
If you adopt your second approach, i.e. relying on the worksheet's format using the chart's "Linked to source" option, then on a trigger of when the drop-down option is changed, you could use VBA to set the format on the worksheet instead of using conditional formatting - then the chart would pick up whatever format you're using.
Please try this:
?Format(1000,"$#,##0.0,")
$1.000,0
?Format(1000,"$#,##0,.0")
$1,0
?Format(1234,"$#,##0,.0")
$1,2

Reinstate Excel chart default resizing behaviour using VBA

I am looking for a way to reinstate the default/native resizing behaviour of a chart in Excel 2010 once it has been disabled (e.g. by manipulating the chart with VBA).
Now I haven't been able to find anything anywhere about the behaviour I have in mind, so I am going to assume that it needs detailed explanation.
Input and select random numerical data into 4-5 cells in Excel, and insert a new Clustered Columns chart. You need to see a the chart's plot area. Now select the chart, and get the PlotArea.Top value with the following line
ActiveChart.PlotArea.Top
If you haven't touched the chart, this should return a value of 7. Now use one of the chart's handlebars to resize the chart vertically, and use the same command line again.
activechart.plotarea.top
Notice how the value returned is still 7. Now set this property to 7 in VBA.
ActiveChart.PlotArea.Top = 7
Again, grab one of the handlebars, resize the chart vertically and get the .top property again using:
ActiveChart.PlotArea.Top
Notice how the value has now changed. It will be either smaller or greater than 7 depending on whetehr you decreased or increased the size of the chart.
Once any element of a chart has been moved either manually or with VBA code, it loses this "absolute position" property and begins moving inside the ChartArea whenever the chart is resized. While some elements can be reset using .SetElement, this does not work for the Plot Area. For example, the following command lines do not reinstate the behaviour I am describing.
ActiveChart.SetElement msoElementPlotAreaNone
ActiveChart.SetElement msoElementPlotAreaShow
I do a lot of automated resizing of charts with VBA, and having the plot area move around by itself makes it a lot harder to predict the effect of resizing the chart and leads to inconstant results.
So back to the question: does anyone know of a way to reinstate this default behaviour, either for the entire chart, or at least specifically for the PlotArea?
Thanks in advance to anyone who may help!
Vincent
I ran into this when I manually resized the plot area and then when the legend is moved it did not resize the plot area at all.
I had tried to save my chart as a template (right click save as template in Excel 2013) but this still had the plot area manually set.
Therefore I would recommend keeping the auto-size behavior before saving a template, since the only way I know to re-set the chart auto-sizing behavior after it has been manually modified is to use a macro
Here is the macro I used to reinstate the auto-sizing behavior
Sub Macro1()
'
' this selects the chart based on the chart name
ActiveSheet.ChartObjects("Chart 4").Activate
' this selects the plot area
ActiveChart.PlotArea.Select
' this clears any custom formatting such as borders or fill colors
ActiveChart.PlotArea.ClearFormats
' this resets the auto-sizing behavior after plot area manually re-sized
ActiveChart.PlotArea.Position = xlChartElementPositionAutomatic
End Sub
References
Why plot area does not expand after clearing series legend?
Excel Chart Plot Area Auto Size - ExcelBanter

Resize Graph to Page

Currently, I have a macro that copies graphs for one workbook, copies it to a worksheet in another workbook every 35 rows apart so that they are all on a new page. With the pasted chart, how do I resize it so that it fits to the page? Right now, there is a bunch of white space around each chart on the page.This is what I have so far.
source.ChartObjects(i).Activate
ActiveChart.ChartArea.Copy
wsTemp.Cells(x, 1).PasteSpecial
x = x + 35
Try moving the chart to a chart sheet instead:
source.ChartObjects(i).Activate
ActiveChart.Location Where:=xlLocationAsNewSheet
Pasting charts 35 rows apart does not ensure each chart gets printed on a new page; you're at the mercy of whoever plays with the page breaks and print settings. By having each chart on its own chart sheet, there's no way [AFAIK] they can ever be printed on the same page. It also ensures each chart fills the entire page.

Acessing chart data in a Picture Type Inline object

I'm currently working on a simple word macro that reads data from an excel spreadsheet and inserts it into specified places in multiple word documents.
Apart from placing the data as text the macro also inserts data into existing charts in the Word template.
To do this im using the InlineShapes object like such:
ActiveDocument.InlineShapes(6).Chart.ChartData.Activate
ActiveDocument.InlineShapes(6).Chart.ChartData.Workbook.Worksheets(1).Range("B2").Value = uwp
ActiveDocument.InlineShapes(6).Chart.ChartData.Workbook.Worksheets(1).Range("C2").Value = uwpsamo
ActiveDocument.InlineShapes(6).Chart.ChartData.Workbook.Worksheets(1).Range("D2").Value = uwpprze
ActiveDocument.InlineShapes(6).Chart.ChartData.Workbook.Worksheets(1).Range("E2").Value = uwprown
ActiveDocument.InlineShapes(6).Chart.ChartData.Workbook.Worksheets(1).Range("F2").Value = uwppodw
ActiveDocument.InlineShapes(6).Chart.ChartData.Workbook.Close
This works fine with one excepton
There is a total of 18 inline objects in my word document, first is of type 3 (picture), after that 16 are of type 12 (chart) and the last one is of type 3 (picture again). The last one is the problem. On top of that picture exists another chart which is not in ActiveDocument.InlineShapes collection
Any idea how to access the chart data in the last inline shape?
InlineShapes.Chart does not work for the last one as the picture type does not have a chart object.