I'm putting together a spreadsheet that dynamically charts a subset data from a large time series. The idea is to allow the user to specify the start time of the data that is charted. This is accomplished using an ActiveX textbox. Additionally, I would like to allow the user to scroll through the time series. This is accomplished by including an ActiveX scrollbar. To keep things synchronous, both the textbox and scrollbar refer to the same cell (a named range called 'datum').
Here is where things get interesting. I've written three subs:
-One updates the time axis of the chart by referring to the datum.
Sub ReScaleChartAxis()
Dim chtMin, chtMax As Long
Dim grphSht, dtSht As Worksheet
Dim minDepthCell As Range
Set grphSht = Sheets("PlotSheet")
Set dtSht = Sheets("Data")
Set minDepthCell = dtSht.Range("datum")
'Optimize
Application.ScreenUpdating = False
Application.EnableEvents = False
'Determine Max/Min of time used in chart
chtMin = Val(minDepthCell.Value)
chtMax = chtMin + 500
'Update chart
With grphSht.ChartObjects("Plot").Chart.Axes(xlCategory)
.MaximumScale = chtMax
.MinimumScale = chtMin
End With
'Reset Optimization
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
-One modifies the datum when the user enters a new value in the textbox.
Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim datMin, datMax As Double
If KeyCode = vbKeyReturn Then
datMin = Sheets("Data").Range("dat_min").Value
datMax = Sheets("Data").Range("dat_max").Value
If Not IsNumeric(TextBox1.Value) Then
TextBox1.Value = Sheets("Data").Range("datum")
ElseIf Val(TextBox1.Value) <= datMin Then
Sheets("Data").Range("datum") = datMin
TextBox1.Value = datMin
ElseIf Val(TextBox1.Value) >= (datMax - 450) Then
Sheets("Data").Range("datum") = datMax - 500
TextBox1.Value = datMax - 500
Else
Sheets("Data").Range("datum") = Val(TextBox1.Value)
End If
End If
End Sub
-One calls the axis rescaling when the scrollbar changes.
Private Sub ScrollBar1_Change()
Call ReScaleChartAxis
TextBox1.Value = Sheets("Data").Range("datum")
TextBox1.Select
End Sub
What is strange is that if the scrollbar is changed by user its event is triggered and the chart updates, AFTER hanging for about 1-2 seconds. Whereas, if the user enters a new value in the textbox, the entry goes through validation, updates the datum cell, then triggers the scrollbar_change event, and completes instantly.
This is a bit of a conundrum to me, to borrow an analogy from a friend, it's as though excel can run 3 miles faster than it can run 2. Does the ActiveX scrollbar have that much overhead with user interaction? I'm attaching an example.
Thank you in advance for any guidance.