Using macros to format charts in excel - excel

I'm trying to create a macro that will format charts in Excel so that all of my charts have the same formatting, but different data. Usually I would just paste the formatting, but these charts were made using a plugin and they do not appear to be compatible with 'paste formats'. Each chart is in a different worksheet along with the chart data.
My charts have 3 series. Each series has a particular format e.g.
series 1: circular marker, size-3pt, no marker line, green fill.
series 2: circular marker, size-3pt, black marker line, yellow fill.
series 3: circular marker, size-3pt, no marker line, red fill.
Also, the charts need to be a specific size for presentation (no sure exact sizes yet, but presumably that should be easy to edit).
These are the only parameters that I need to change from the default in order to get the charts to look how I want them. Can anyone help?

Chart formatting can be done fairly easily with VBA:
Dim chChart as chart
set chChart = Thisworkbook.Sheet("Sheet1").ChartObjects("Chart1").Chart
With chChart
seriescollection(1).Markerforegroundcolor = rgb(255,255,255)
seriescollection(1).Markerbackgroundcolor = rgb(255,255,255)
seriescollection(1).interior.color = rgb(255,255,255)
End With
The above code should give you an idea of how to write the macro, substituting the correct sheet and chart names. Ideally you would also use some loops to loop through you charts and series within the charts
For each chart in Thisworkbook.Sheet("Sheet1").ChartObjects
For each series in chChart.seriescollection
To get the exact values you want to change I would suggest either recording a macro of you making the changes manually or using the Editor Object Browser (F2 in the VBA Editor) to find the likely values.

Related

Is possible to mirror a chart in another place?

I have a chart on one excel worksheet, and I want to show it in another worksheet without moving the original chart.
I do not want to copy and paste it, because if I modify the original chart (for example, changing his format), it will not be reproduced in the copy.
Is possible to hack it to show the chart in another page? Maybe a picture whose image is automatically taken from the original chart?
I frequently modify the original chart, and I have another worksheet where I watch many charts at once.
This can be done with a dynamic image, without VBA.
It will work best if the original chart is perfectly aligned with a range of cells.
select the range of cells behind the chart, for example A1:D10. You may want to give that range a name with the Name Manager, i.e. myChart, for convenience
on the Home ribbon, click the Copy dropdown and select "Copy as Picture"
accept the defaults "as shown on screen" and "picture" and hit OK
in the target worksheet (in the SAME workbook), select a cell and paste from the clipboard
select the pasted image and click into the formula bar. Enter the cell range you copied it from into the formula bar, e.g. =Sheet1!$A$1:$D$10, or, if you have set up a name, use that, like =myChart
The image is now dynamic and will reflect whatever content is showing in the specified cell range. If you resize the original cells, the dynamic image will distort.
Be aware that you are not creating an image of the actual chart, but only of the cells that the chart covers. If you move the chart away from the range, then it will not be visible in the dynamic image.
That's why this technique works best if the chart is perfectly aligned with a range of cells.
More details about this technique, using it to create combination Sparklines is in this article written in 2011, but everything still applies.

Copy all charts from excel into an existing powerpoint

I have 35 excel files that each have 50 charts in them (all on the same sheet). I need to copy each of the charts over to a powerpoint file, which is already made with specific formatting and labels for positioning of the charts. How would I go about writing a script that will reference all of the charts in the respective excel file and then send them to the powerpoint at specific locations and page numbers of the powerpoint, some being differently sized?
I am assuming a forloop for referencing each of the charts, then an explicit code for each chart once numbered in the forloop to resize and place at specifc locations in the powerpoint.
Once I get an idea of how to reference each of the charts, resize and then place at specific point in the ppt slide, I can automate the entire thing.
Please note that StackOverflow is not a coding service. Bearing that in mind this is how I would go about it:
1. Put placeholders in the PowerPoint. These placeholders contain a unique identifier of each chart object (e.g. Workbook1.Sheet1.Chart1). I would make sure they are sized as your chart would be in the end (width & height) and that they are invisible. I would use rectangles with 100% transparency, and use the Alt Text property.
2. Write a sub that deletes all charts but keeps all placeholders in your PowerPoint file.
3. Write a function that retrieves the charts per placeholder.
4. Write a sub that copies the chart object and pastes it as a picture.
5. Write a sub that loops through all placeholders in your file, gets and pastes all charts.

How can I set a default value to the x and y dimensions of a copied object from excel into autocad?

I would like to set a value for the default size of the OLE object when I copy and paste a range of cells into autocad from excel.
Normally, when I copy and paste from excel into autocad, in autocad, i have to mess with the scaling to get it just right. This takes unnecessary time.
Instead, I want excel to paste this range of cells at a certain height and width. I'm curious if there are properties in excel or autocad where I can set the default x and y dimensions of the copied object.
The range of cells to be copied in excel are always the same, but it just will have different data in it each job.
You can't do that on-the-fly unless you made yourself a sophisticated lisp.
For me I do all the adjustments I need on one ole object only (either scaling or aligning it to a line or a rectangle). I copy this object multiple times in the drawing then I edit these copies (OLE > OPEN) by copying the ranges I need from Excel into them.

Inconsistent colors in Excel/PowerPoint/Word Charts

I have two scatter plots, representing the same things, but for two different scenarios. They were created in the same Excel workbook, on different sheets, and I have the same color schemes applied to them. However, the colors don't line up between the charts. I have 6 charts total, and 4 start with the dark color and the other 2 start with a lighter color.
Chart 1 - first color is very dark
Chart 2 - first color is lighter
I haven't been able to find anyone else who's run into the same problem. This issue occurs when I copy the charts into PowerPoint and Word as well. This is pretty annoying because I want to keep the colors consistent throughout my charts.
EDIT I can copy the chart with the colors I want and then just edit the data, so the charts are all consistent. Still not sure how they ended up different colors, though.
I've run into this issue in the past. I've looked around and found the following suggestions.
After copying the chart from Excel, when you're in either Powerpoint or Word, click on the downward arrow for "paste", then try to do any one of the following:
'paste special' then 'paste link'; or
'paste options' then 'keep source formatting'; or
'paste options' then paste as 'picture'.
The last one should definitely work, since you're pasting a static photo. That is, it will be uniform regardless of the program because it's based on the calibration setting of your monitor.

Excel macro changed ActiveChart.SeriesCollection(1).XValues but the text remained unchanged

I am very new to VB or VBA, but recent work with Excel requires a huge number of repetitions. That's why I am now considering the VBA macro approach to simplify the process.
The work is simple - to generate charts with given data. But there are hundreds of charts to create, so I used copy and paste to keep the style consistency, and changed the values within afterwards.
Everything worked out pretty well with the data, but not so smooth with the axis label. I was tring to use ActiveChart.SeriesCollection(1).XValues = "=<sheet_name>!$<row_num>$<col_num>" to modify the value to some existing text in the respective cells, but it didn't work out on the chart, i.e., if the label was "total", as is in the A6 cell, in the original chart that I copied from, after selecting the pasted chart and executing the command above, the label in the new chart still says "total" instead of what's in the, say, B6 cell, which for example is "China". But when I right clicked the chart and went into "select data", the "Horizontal (Catagory) Axis Label" is indeed changed into "=sheet1!$B$6", which made me very confused.
Anyone knows how this happened and how to fix this? Is there an "update" method to the chart object or anything that I've been missing?
I am currently using Excel 2010.
Welcome to the strange world of Excel VBA.
SeriesCollection().XValues requires either a Range object or an array of points, so you must transform your string into a range:
ActiveChart.SeriesCollection(1).XValues = Sheets(<sheet_name>).Range("$<row_num>$<col_num>")

Resources