excel vba code charts

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Excel VBA code for charts

Excel VBA code for charts

resolvedResolved · High Priority · Version 2010

Fabio has attended:
Excel VBA Advanced course

Excel VBA code for charts

Is there any way we could write a code which clicking on a chart trace, dragggin the mouse would zoom in or out of the graph? This is a nice feature that many softwares like Matlab offer.

I was using a Class Module to monitor events on an embedded chart to select and move different traces but still could not find a way of selecting a specific area and zooming in and out.

For example... drawing a rectangle around a specific area of the graph would bring the trace inside the rectangle to fill the whole chart area.

Thank you


RE: Excel VBA code for charts

Hi Fabio

Thanks for getting in touch. I'm not completely certain what you mean by "trace". Are you able to link me to examples online demonstrating the functionality, possible a video clip etc?

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Excel VBA code for charts

Hi Gary,

I found this short video on youtube which shows what I am trying to replicate in Excel.

http://youtu.be/AoqQKHLEW-E

I was using chart events to find out which series of the chart I was selecting with the mouse and later moving the series up and down, side to side and rotating it. So I guess we would have to try something similar to monitor where the mouse cursor is when we click on it and get its coordinates with something similar to:
MyChart.GetChartElement x, y, ElementId, arg1, arg2

That's what I was using in my class module, but to avoid complications with class modules we could just work with a chart sheet instead of embedded ones.

I was thinking about a combination of chart object events:
MouseDown, MouseMove and MouseUp for this task but I am not sure how...

Thank you.

RE: Excel VBA code for charts

Hi Fabio

Thanks for the video, that really helped. It's not something I've ever tried to do and wasn't completely confident which approach to take. However searching around I found some enlightening forum threads, such as this one:

http://www.mrexcel.com/forum/excel-questions/535478-how-zoom-charts-using-visual-basic-applications.html

And as I suspected, chart guru Jon Peltier has created a utility that does exactly this, although he has had his fair share of issues:

http://peltiertech.com/WordPress/pts-charts-in-excel-2010/

He is a very well known authority on Excel charts. If *he's* had problems trying to make it work, I would recommend downloading one of his utilities and making use of his code.

I hope this helps and you manage to get the feature you are after.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Excel VBA code for charts

Hi Gary,

I've contacted Peltier and he didn't solve those issues after 2003 and when he does wouldn't be willing to give the code away which is understandable.

I found a way around it though using some imagination...

It works really well for what I need here at work having to zoom in and out of small areas many times.

I set up the chart image as a background in the spreadsheet, and selecting the cells in the spreadsheet I could compare it with the current max and min scales to have them changed in the original chart and setting the image of the new chart as a background again.
The areas around your chart I simply made it white because you have chart images all over the spreadsheet.
Sounds complicated but it's very simple.
To make the selection quite refined I set up the column width as 0.5 and row height as 5, but you can go even lower. To look neater I've hidden the row and column headers.
I've got some extra code to bring it back to auto scale etc.. but just left out of the script to leave only the basic idea.
The ideal would be setting something with chart event mouse down, mouse move, mouse up but the issues related with this code is not solved and for now this is all I need.
The code which you put in the worksheet to monitor selection change looks like this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Monitors only the plotting area (change it to suit your chart)
If Not Intersect(Target, Range("I9:GD186")) Is Nothing Then
'Defines limits of rows of the chart which you can change
FirstRowOfChartArea = 9
LastRowOfChartArea = 99
TotalChartRows = LastRowOfChartArea - FirstRowOfChartArea

'Defines limits of columns of the chart which you can change to suit your chart
FirstColumnOfChartArea = 9
LastColumnOfChartArea = 186
TotalChartColumns = LastColumnOfChartArea - FirstColumnOfChartArea

'Monitors selected area rows
FirstRowSelectedInChartArea = Selection.Cells(1, 1).Row
LastRowSelectedInChartArea = Selection.Rows.Count + FirstRowSelectedInChartArea - 1

'Monitors selected area columns
FirstColumnSelectedInChartArea = Selection.Cells(1, 1).Column
LastColumnSelectedInChartArea = Selection.Columns.Count + FirstColumnSelectedInChartArea - 1

ActiveSheet.ChartObjects("Chart1").Activate
'Gets values of min and max scales from the chart
MinYScale = ActiveChart.Axes(xlValue).MinimumScale
MaxYScale = ActiveChart.Axes(xlValue).MaximumScale

MinXScale = ActiveChart.Axes(xlCategory).MinimumScale
MaxXScale = ActiveChart.Axes(xlCategory).MaximumScale

'Checks in x and y directions the number of points per cell
YPointsPerDivision = (MaxYScale - MinYScale) / TotalChartRows
XPointsPerDivision = (MaxXScale - MinXScale) / TotalChartColumns

'Calculates new X min and max values
NewMaxY = MaxYScale - ((FirstRowSelectedInChartArea - FirstRowOfChartArea) * YPointsPerDivision)
NewMinY = MinYScale + ((LastRowOfChartArea - LastRowSelectedInChartArea) * YPointsPerDivision)

'Calculates new Y min and max values
NewMaxX = MaxXScale - ((LastColumnOfChartArea - LastColumnSelectedInChartArea) * XPointsPerDivision)
NewMinX = MinXScale + ((FirstColumnSelectedInChartArea - FirstColumnOfChartArea) * XPointsPerDivision)

'Sets min and max scales to the new calculated values
ActiveSheet.ChartObjects("Chart1").Activate
'X Values
ActiveChart.Axes(xlCategory).MinimumScale = Round(NewMinX, 0)
ActiveChart.Axes(xlCategory).MaximumScale = Round(NewMaxX, 0)

'Y Values
ActiveChart.Axes(xlValue).MinimumScale = Round(NewMinY, 1)
ActiveChart.Axes(xlValue).MaximumScale = Round(NewMaxY, 1)

'Saves the chart as a jpeg and puts the picture in the background
Dim ForcePic As String
ForcePic = Environ$("temp") & "\Chart1.jpg"
ActiveWorkbook.Worksheets("Sheet1").ChartObjects("LVDT").Chart.Export _
Filename:=ForcePic, FilterName:="JPG"
Sheets("Sheet1").Activate
ActiveSheet.SetBackgroundPicture ForcePic
Kill ForcePic
End If
End Sub

Hope it helps
Regards
Fabio


RE: Excel VBA code for charts

Hi Fabio

Glad you made some progress. The saving as JPEG is inspired, well done!

I will certainly give this a try to see how it works. Very impressive.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Excel VBA code for charts

If you have an e-mail address I could send the spreadsheet you could try and see how it works.

Regards
Fabio

RE: Excel VBA code for charts

Hi Fabio

That's very kind of you, it sounds very interesting. I'd love to take a look.

My address is gary@stl-training.co.uk.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Tue 19 Feb 2013: Automatically marked as resolved.


 

Excel tip:

Recently used file list

Under the File menu, you may find a list of files at the bottom of the menu. These files represent the most recently used Excel spreadsheets. This file list provides a quick way for you to access your files.

You can disable the file list feature of Excel. This is done by

1. Choose Tools > Options menu. You will see the Options dialog box.
2. Ensure the General tab is selected.
3. Make sure the Recently Used File List check box is cleared.
4. Click on OK.

View all Excel hints and tips


Server loaded in 0.07 secs.