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>")
Related
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.
I have a function entered in the name manager that is as follows:
=OFFSET(INDIRECT(ADDRESS(10,1,1,1)), rowOffset, startColumn + StartYear - firstColumnYear-1,1,yearsOfData+1)
It is a formula that will dynamically update a line plot of investment market values between two years that are selected by the user. When I select the entry in name manager, I can see that excel clearly makes the correct selection but when I try to enter the name in an excel graph, excel says "We found a problem with one or more references in this worksheet". I have no idea what the problem I am running into is caused by. I have used similar methods in the past to make dynamic graphs without issue. Thanks for any help you can provide and let me know any other info I can offer to help find a solution.
Seems as if you try to be sheet-independent with your name and INDIRECT(ADDRESS(10,1,1,1)). This is not possible for charts. If you replace the INDIRECT Part with Sheet1!$A$10 in your name definition, then it should work. In this example Sheet1 is the sheet where the data for the chart are.
The reason is, there is no ThisSheet for the chart from which it can get its data. Even not the sheet, the chart is placed on. The chart can be placed on a sheet but it can also be placed in a separate chart sheet. So the data definition has to be complete with sheet and range.
Further there seems to be a second problem. INDIRECT with ADDRESS not works for names for chart series. So if the goal is to have the start address dynamic, we have to construct it with string concatenation in INDIRECT. Because
=OFFSET(INDIRECT("Sheet1!$A$10"),1,1,1,5)
works.
I am currently working on an excel spreadsheet called Calendar.xlsm
I have Excel2010.
My workbook has the following worksheets:
Cover Sheet
Calendar
Colour Detection
Printable
The "Colour Detection" sheet uses a User Defined Function to detect the colours of cells on the "Calendar" worksheet and based on that colour returns a number for that particular cell; For example if Calendar!C3 has a fill colour of "light green" then Colour Detection!C3 = 43
Just for completeness the UDF is called ColorIndex and was written by Bob Phillips.
I then use conditional formating to colour cells in my "Printable" worksheet based on the number stored in "Colour Detection".
My problems start when there is a colour change on the "Calendar" sheet, usually when this happens "Colour Detection" remains unchanged unless I select the cells manually and press return.
My knowledge of how Excel works with Visual Basic in this area is very limited but growing.
My first idea for a solution was to record a macro where I selected each cell and hit return one by one. I then attached the macro to a button called Calculate on the "Cover Sheet". Immediately this didn't work because there are approximately 800 cells that use this UDF.
While trying this I thought I stumbled on the solution, I noticed that if I applied my solution to only a few cells, the whole worksheet seemed to update. I then applied this and tested it a few times and it worked. I then presented my work to a friend as the masterpiece I was sure it was, and while demonstrating it to them it all fell over and only the cells I selected in my macro updated.
Next I tried adding the line
Application.Volatile
to my UDF but this doesn't to make a difference.
My question is how do I easily force every single cell in my worksheet to update by doing something simple like pushing a recalculate button?
Try Ctrl-Alt-F9 this forces every formula in the workbook to be calculated. Or you could set workbook.forcefullcalculation to True in your workbook_Open event.
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.
I run live market charts in Excel. A graph indicator runs from live data fed by the broker through the DDE facility in Microsoft Excel. It works fine and I am happy with it - except for one blemish I hope you can assist with.
The lines of the graph (it is a line chart type) are created in real time. The present time and the past time are great, very clean. Unfortunately in the period just ahead i.e. that hasn't arrived yet, the lines of the graph drop to zero and crawl along the x-axis into the future. This spoils the current reading of the graph.
Is there any way I can prevent this happening so that the lines (properly called curves) only exist in the past and current time period. The worksheet is set not to show zero values in formulae, but the charting facility does not appear to have this function.
If you put #N/A in the cell (using the =NA() function) the points are not drawn.
To hide the ugliness of your cells now having #N/A in them for future dates you can use a number format or conditional formatting to hide error values. You do this by adding a formula to your conditional formatting of
=ISNA(A1)
and setting the format to be a white font on a white background. Which makes the cells with #N/A in them appear blank. For versions of Excel prior to conditional formatting being available you can do the same thing with a number format but in reverse. i.e., set your font to white so all cells are invisible by default and set your number format to:
[black]0.0;[red]-0.0;[black]0.0;[black]
which should make anything that isn't an error appear with either a black or red font colour.
Regards
Dave
I once saved an Excel 97 Worksheet (with charts) in Excel 2003. After that, whenever I saved the file in '97, I got a prompt telling me that the spreadsheet was created in a newer version of Excel.
To eliminate the prompt I opened it in 2003 and saved it as an Excel 5.0/95 Workbook (*.xls). Then whenever I opened it in 97 the line charts dropped to zero at the end. They didn't do that before.
To correct the problem I right-clicked on the chart, selected Chart type, selected Custom Types, then "Colored Lines". Apparently when I saved it as Excel 5.0/95 that chart type was not supported, so the program defaulted to the type "Smooth Lines". After changing the chart type to "Colored Lines" it was only necessary to do some minor formatting.
This experience suggests a solution to the line dropping to zero problem regardless of what triggered it. Simply change the chart type. When I did that the dropped line disappeared and I didn't have to use the #N/A approach.