How to zoom on charts using VBA

Rasm

Well-known Member
Joined
Feb 9, 2011
Messages
505
Just wondering if Excel has a zoom function where I can simply highlight a area on a chart - by click and drag kinda deal - then that area is zoomed in on.

I want to do this using VBA
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I use this for embedded charts
Code:
Sub ZoomInAndOut()

    Select Case ActiveSheet.Name
        Case "Chart 1", "Chart 2", "Chart 3"
            If ActiveWindow.Zoom = 89 Then
        ActiveWindow.Zoom = 400  '<-----Change to suit
    Else
ActiveWindow.Zoom = 89

End If

Case Else

MsgBox "You cannot use this feature on this sheet." & vbLf & _
    "Use is restricted to the following sheets:" & vbLf & _
        """Chart 1"", ""Chart 2"" and ""Chart 3""", vbOKOnly, "Restricted Use"
    
End Sub

Maybe it helps.

John
 
Upvote 0
Mostly no but kinda' yes.... sort of.

Here are the Excel 2007 zoom tools ...
Zooming into Your Excel 2007 Worksheets

The "Fit Selection" zoom feature doesn't work on a chart selection. Only on a cell selection. You could however select the cells surrounding your chart and then select the "Fit Selection" zoom feature and there is your kinda' yes solution.
 
Upvote 0
In the sheet module:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    UnZoom
End Sub

In a code module:
Code:
Option Explicit
 
Dim miRow As Long
Dim miCol As Long
Dim mfZoom As Single
Dim mbZoom As Boolean
 
Sub ChartZoom()
    If Not mbZoom Then
        With ActiveWindow
            miRow = .ScrollRow
            miCol = .ScrollColumn
            mfZoom = .Zoom
        End With
        mbZoom = True
    End If
    
    With ActiveSheet.ChartObjects(Application.Caller)
        With Range(.TopLeftCell, .BottomRightCell)
            Application.EnableEvents = False
            .Select
            Application.EnableEvents = True
            ActiveWindow.ScrollRow = .Row
            ActiveWindow.ScrollColumn = .Column
        End With
        ActiveWindow.Zoom = True
        .Select
    End With
End Sub
 
Sub UnZoom()
    If mbZoom Then
        With ActiveWindow
            .ScrollRow = miRow
            .ScrollColumn = miCol
            .Zoom = mfZoom
        End With
        mbZoom = False
    End If
End Sub

Select each chart on the worksheet and assign macro ChartZoom.
 
Upvote 0
I am not very familiar working with charts. Isn't there a UI option or VBA to zoom an embeeded chart without having to zoom the underlying worksheet ?
 
Upvote 0
No, sadly. Jon P was working on a zoom utility (changing the chart scales based on a rectangle) but the chart events in 2007+ are a little flaky - see here.
 
Upvote 0
No, sadly. Jon P was working on a zoom utility (changing the chart scales based on a rectangle) but the chart events in 2007+ are a little flaky - see here.

Thanks Rory. This sounds like a good candidate for an API workaround... like giving the user the ability to ,say for example, zoom the chart when moving the mouse pointer over it. Would be nice for small charts in a crowded worksheet.
 
Upvote 0
I can't wait to see what you come up with. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top