RichardWale
Technical User
I had a post on the MS Office forum about how to interrogate an Excel Scatter Chart point so that I could display more information about the record that had created the point.
Skip Vought replied immediately with a cracking answer, which I have incorporated and works very well, with one exception, in that in one specific circumstance Excel crashes. I am running Excel 2010.
I have tried to put in 'Error Trapping' and that appeared to work, but it still crashes sometimes.
When a point is selected, I have created an option to go to the 'Enquiry' screen for more details about the item that the point represents, or stay on the 'Cosmos Chart' and select another point - it is the second 'No' option that seems to be causing the problem.
I wondered if it is something to do with the mouse and 'focus', so tried putting in some 'SendKeys ("{ESC}")' lines into the code, but that appeared to cause more problems and the macro would 'hang' - seems to be a well-known issue with VBA? I have taken most of the 'SendKeys ("{ESC}")' lines out of the code.
A full working version of the Stock Model is attached, and the sequence that triggers a crash seems to be:
- Open the analysis and enable macros
- From the worksheet tabs, select 'Cosmos Chart'
- Click on any point and choose 'No'
- Chart now has multiple data points 'highighted' - how does this happen?
- Repeat the data point selection and 'No' and only a single point is highlighted, the one selected
- Repeat the point selection and 'No' a number of times and Excel crashes, AND at the point that it crashes turns off the keypad 'Num Lock' - that is really bizarre! Sometimes it crashes after the first point and 'No' have been selected
- Selecting 'Yes', AND using the 'Cosmos Chart' button at the top right of the 'Enquiry' screen, then selecting a point and 'No', does not appear to create any problems - even repeating the complete cycle of 'Yes' and 'No' a number of times
- Selecting 'Yes' and then returning directly to the 'Cosmos Chart' worksheet by selecting the worksheet tab, AND then selecting a point and 'No' can cause a crash
- Selecting 'Yes' and then returning directly to the 'Cosmos Chart' worksheet by selecting the worksheet tab, AND then selecting a point and 'Yes' appears to be fine
I have tried replicating the steps that the 'Yes' option takes as part of the 'No' option in the macro, but it still crashes fairly predictably.
This is the code, but the full working Excel file is attached:
Option Explicit
Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
'Skip Vought 2016 Nov 18, added to by Richard Wale 19 Nov 2016
'click on a data point and chose bewteen selecting another data point,
'or viewing the selected part number in the 'Item Enquiry' screen
Dim IDNum As Long
Dim a As Long
Dim b As Long
Dim xl As Application, ws As Worksheet
Dim itemrefrange As String
Dim itemref As String
Dim response1 As String
Dim itemdesc As String
Sheets("Cosmos Chart").Select
itemrefrange = Sheets("Formulae").Range("A1203").Value
With ActiveWorkbook.Names("Item_Ref")
.Name = "Item_Ref"
.RefersToR1C1 = itemrefrange
.Comment = ""
End With
Set xl = Application
Set ws = Sheets("Cosmos Data")
Sheets("Cosmos Chart").Select
ActiveChart.GetChartElement x, y, IDNum, a, b
If IDNum = xlSeries Then
itemref = xl.Index(ws.Range("Item_Ref"), 1, b) 'log Item Ref
End If
Sheets("Formulae").Select
Range("A1208").Value = itemref
If IsError(Range("A1209").Value) Then 'clicking on non-data points creates an error
GoTo LabelMsg
End If
itemdesc = Range("A1209").Value 'find item description
Sheets("Cosmos Chart").Select
response1 = MsgBox("Item: " & itemref & Chr(10) & "Desc: " & itemdesc _
& Chr(10) & Chr(10) & "Press 'Yes' to continue to 'Item Enquiry' Screen" _
& Chr(10) & Chr(10) & "or" & Chr(10) & Chr(10) & _
"Press 'No' to select another point on the Cosmos Chart", _
vbYesNo + vbDefaultButton1, "Stock Model 2010 - Cosmos Chart Enquiry")
Set ws = Nothing
Set xl = Nothing
If response1 = vbYes Then
Sheets("Cosmos Data").Select
Cells(1, b + 15).Select 'select appropriate item record
Call System_Macros.Enquiry5
GoTo Labelend
End If
Sheets("Cosmos Data").Select 'need to select different ws to avoid Excel error
Cells(1, b + 15).Select 'select appropriate item record
Call System_Macros.Enquiry5
Call System_Macros.Cosmos_Chart
GoTo Labelend
LabelMsg:
Sheets("Cosmos Chart").Select
MsgBox "Only click on data points" & Chr(10) & Chr(10) & _
"Anywhere else can cause errors", , "Cosmos Chart Warning"
Call System_Macros.Cosmos_Chart
Labelend:
End Sub
Can anyone see anything that I have done that could cause this please?
It's so infuriating, as the data point 'click to enquire' works really well otherwise.
If there is no answer to the problem, I will have to remove the Yes/No option and just default to the 'Enquiry' screen after selecting a data point.
Skip Vought replied immediately with a cracking answer, which I have incorporated and works very well, with one exception, in that in one specific circumstance Excel crashes. I am running Excel 2010.
I have tried to put in 'Error Trapping' and that appeared to work, but it still crashes sometimes.
When a point is selected, I have created an option to go to the 'Enquiry' screen for more details about the item that the point represents, or stay on the 'Cosmos Chart' and select another point - it is the second 'No' option that seems to be causing the problem.
I wondered if it is something to do with the mouse and 'focus', so tried putting in some 'SendKeys ("{ESC}")' lines into the code, but that appeared to cause more problems and the macro would 'hang' - seems to be a well-known issue with VBA? I have taken most of the 'SendKeys ("{ESC}")' lines out of the code.
A full working version of the Stock Model is attached, and the sequence that triggers a crash seems to be:
- Open the analysis and enable macros
- From the worksheet tabs, select 'Cosmos Chart'
- Click on any point and choose 'No'
- Chart now has multiple data points 'highighted' - how does this happen?
- Repeat the data point selection and 'No' and only a single point is highlighted, the one selected
- Repeat the point selection and 'No' a number of times and Excel crashes, AND at the point that it crashes turns off the keypad 'Num Lock' - that is really bizarre! Sometimes it crashes after the first point and 'No' have been selected
- Selecting 'Yes', AND using the 'Cosmos Chart' button at the top right of the 'Enquiry' screen, then selecting a point and 'No', does not appear to create any problems - even repeating the complete cycle of 'Yes' and 'No' a number of times
- Selecting 'Yes' and then returning directly to the 'Cosmos Chart' worksheet by selecting the worksheet tab, AND then selecting a point and 'No' can cause a crash
- Selecting 'Yes' and then returning directly to the 'Cosmos Chart' worksheet by selecting the worksheet tab, AND then selecting a point and 'Yes' appears to be fine
I have tried replicating the steps that the 'Yes' option takes as part of the 'No' option in the macro, but it still crashes fairly predictably.
This is the code, but the full working Excel file is attached:
Option Explicit
Private Sub Chart_MouseDown(ByVal Button As Long, ByVal Shift As Long, ByVal x As Long, ByVal y As Long)
'Skip Vought 2016 Nov 18, added to by Richard Wale 19 Nov 2016
'click on a data point and chose bewteen selecting another data point,
'or viewing the selected part number in the 'Item Enquiry' screen
Dim IDNum As Long
Dim a As Long
Dim b As Long
Dim xl As Application, ws As Worksheet
Dim itemrefrange As String
Dim itemref As String
Dim response1 As String
Dim itemdesc As String
Sheets("Cosmos Chart").Select
itemrefrange = Sheets("Formulae").Range("A1203").Value
With ActiveWorkbook.Names("Item_Ref")
.Name = "Item_Ref"
.RefersToR1C1 = itemrefrange
.Comment = ""
End With
Set xl = Application
Set ws = Sheets("Cosmos Data")
Sheets("Cosmos Chart").Select
ActiveChart.GetChartElement x, y, IDNum, a, b
If IDNum = xlSeries Then
itemref = xl.Index(ws.Range("Item_Ref"), 1, b) 'log Item Ref
End If
Sheets("Formulae").Select
Range("A1208").Value = itemref
If IsError(Range("A1209").Value) Then 'clicking on non-data points creates an error
GoTo LabelMsg
End If
itemdesc = Range("A1209").Value 'find item description
Sheets("Cosmos Chart").Select
response1 = MsgBox("Item: " & itemref & Chr(10) & "Desc: " & itemdesc _
& Chr(10) & Chr(10) & "Press 'Yes' to continue to 'Item Enquiry' Screen" _
& Chr(10) & Chr(10) & "or" & Chr(10) & Chr(10) & _
"Press 'No' to select another point on the Cosmos Chart", _
vbYesNo + vbDefaultButton1, "Stock Model 2010 - Cosmos Chart Enquiry")
Set ws = Nothing
Set xl = Nothing
If response1 = vbYes Then
Sheets("Cosmos Data").Select
Cells(1, b + 15).Select 'select appropriate item record
Call System_Macros.Enquiry5
GoTo Labelend
End If
Sheets("Cosmos Data").Select 'need to select different ws to avoid Excel error
Cells(1, b + 15).Select 'select appropriate item record
Call System_Macros.Enquiry5
Call System_Macros.Cosmos_Chart
GoTo Labelend
LabelMsg:
Sheets("Cosmos Chart").Select
MsgBox "Only click on data points" & Chr(10) & Chr(10) & _
"Anywhere else can cause errors", , "Cosmos Chart Warning"
Call System_Macros.Cosmos_Chart
Labelend:
End Sub
Can anyone see anything that I have done that could cause this please?
It's so infuriating, as the data point 'click to enquire' works really well otherwise.
If there is no answer to the problem, I will have to remove the Yes/No option and just default to the 'Enquiry' screen after selecting a data point.