' Original settings
Private MajUnit As Double
Private MajTickCount As Long
Private MaxScale As Double
Private MinScale As Double
Private ScaleRange As Double
Private cht As Chart
' Forms objects
Private scrZX As ScrollBar ' X axis scale zoom
Private lblZ As Label ' X axis "Zoom" label
Private scrRX As ScrollBar ' X axis scale range
Private lblR As Label ' X axis "Range" label
Private btnReset As Button ' Reset button
Private btnClose As Button ' Close button
Sub Zoomer_Initialize()
Dim rngSeed As Range, ChartCellWidth As Integer
Set cht = ActiveChart
' ref points on sheet for controls
Set rngSeed = ActiveSheet.Cells(cht.Parent.BottomRightCell.Row + 2, cht.Parent.TopLeftCell.Column)
ChartCellWidth = cht.Parent.BottomRightCell.Column - cht.Parent.TopLeftCell.Column
' Add controls
With rngSeed
Set lblZ = ActiveSheet.Labels.Add(.Left, .Top, .Width, .Height)
lblZ.Caption = "Zoom"
With .Offset(1)
Set lblR = ActiveSheet.Labels.Add(.Left, .Top, .Width, .Height)
lblR.Caption = "Range"
End With
With .Offset(, 1).Resize(, ChartCellWidth - 2)
Set scrZX = ActiveSheet.ScrollBars.Add(.Left, .Top, .Width, .Height)
scrZX.OnAction = "scrZX_Change"
End With
With .Offset(1, 1).Resize(, ChartCellWidth - 2)
Set scrRX = ActiveSheet.ScrollBars.Add(.Left, .Top, .Width, .Height)
scrRX.OnAction = "scrRX_Change"
End With
With .Offset(, ChartCellWidth - 1).Resize(2)
Set btnReset = ActiveSheet.Buttons.Add(.Left, .Top, .Width, .Height)
btnReset.Caption = "Reset"
btnReset.OnAction = "btnReset_Click"
End With
With .Offset(, ChartCellWidth).Resize(2)
Set btnClose = ActiveSheet.Buttons.Add(.Left, .Top, .Width, .Height)
btnClose.Caption = "Close"
btnClose.OnAction = "btnClose_Click"
End With
End With
With cht.Axes(xlCategory)
'.MinimumScaleIsAuto = True
MaxScale = .MaximumScale
MinScale = .MinimumScale
ScaleRange = .MaximumScale - .MinimumScale
MajUnit = .MajorUnit
MajTickCount = ScaleRange / MajUnit
' Zoom X
scrZX.Max = MajTickCount
scrZX.Min = 1
scrZX.SmallChange = 1
scrZX.LargeChange = 1
scrZX.Value = 1
' Range X
scrRX.Max = 1
scrRX.Min = 1
scrRX.SmallChange = 1
scrRX.LargeChange = 1
scrRX.Value = 1
End With
End Sub
Private Sub scrRX_Change()
With cht.Axes(xlCategory)
.MinimumScale = MinScale + (MajUnit * (scrRX.Value - 1))
.MaximumScale = .MinimumScale + (MajUnit * (MajTickCount - scrRX.Max + 1))
End With
End Sub
Private Sub scrZX_Change()
With cht.Axes(xlCategory)
.MaximumScaleIsAuto = False
scrRX.Max = scrZX.Value
.MinimumScale = MinScale + (MajUnit * (scrRX.Value - 1))
.MaximumScale = .MinimumScale + (MajUnit * (MajTickCount - scrRX.Max + 1))
End With
End Sub
Private Sub btnReset_Click()
With cht.Axes(xlCategory)
.MaximumScale = MaxScale
.MinimumScale = MinScale
.MajorUnit = MajUnit
End With
scrZX.Value = 1
scrRX.Max = 1
scrRX.Min = 1
scrRX.Value = 1
End Sub
Private Sub btnClose_Click()
btnReset_Click
scrZX.Delete
scrRX.Delete
btnReset.Delete
btnClose.Delete
lblZ.Delete
lblR.Delete
End Sub