Consulting

Results 1 to 8 of 8

Thread: Solved: Chart Labels need to be dynamic?

  1. #1

    Solved: Chart Labels need to be dynamic?

    Good day to all you VBA gurus.

    I have a chart that contains the answers to a range of questions (divided into 5 categories), and due to the length of the questions the X - axis only contains the label "Question 1", "Question 2" etc. up to "Question 34".

    Courtesy of Jon Peltier & Ian Coulson I have managed to cobble together the VBA code below, that allows a user to see the question in full (in a mesage box) when they click on any of the data points on the chart.

    This code works brilliantly when all questions are shown, however if the user selects to view only questions belonging to one category (user clicks a button which hides the all rows not required on data sheet, thus displaying on graph on relevant qustions), the message box no longer displays the right question. For example if the user chooses to see questions relating to transport, which are questions 12 - 16, the user sees the results for the qestion correctly, but the message box displays the full questions for questions 1 - 5).

    Does anyone know how I can adapt the code so it will show the correct questions when the filtering is done?

    [vba]Private Sub Chart_MouseUp(ByVal Button As Long, ByVal Shift As Long, _
    ByVal x As Long, ByVal y As Long)

    Dim ElementID As Long, Arg1 As Long, Arg2 As Long
    Dim myX As Variant, myY As Double
    With ActiveChart
    ' Pass x & y, return ElementID and Args
    .GetChartElement x, y, ElementID, Arg1, Arg2

    ' Did we click over a point or data label?
    If ElementID = xlSeries Or ElementID = xlDataLabel Then
    If Arg2 > 0 Then
    ' Extract x value from array of x values
    myX = WorksheetFunction.Index _
    (.SeriesCollection(Arg1).XValues, Arg2)
    ' Extract y value from array of y values
    myY = WorksheetFunction.Index _
    (.SeriesCollection(Arg1).Values, Arg2)

    ' Display message box with point information
    MsgBox "Point " & Arg2 & vbCrLf _
    & " - " & Worksheets("Data").Range("H17:H50").Cells(Arg2, 1).Value
    End If
    End If
    End With

    End Sub
    [/vba]

    Thanks in advance for any & all assistance given.

    Edited to add sample file
    Attached Files Attached Files
    Last edited by MRichmond; 08-15-2011 at 11:36 PM. Reason: To add test file

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub Chart_MouseUp(ByVal Button As Long, ByVal Shift As Long, _
    ByVal x As Long, ByVal y As Long)
    Dim ElementID As Long, Arg1 As Long, Arg2 As Long
    Dim myX As Variant, myY As Double

    With ActiveChart

    ' Pass x & y, return ElementID and Args
    .GetChartElement x, y, ElementID, Arg1, Arg2

    ' Did we click over a point or data label?
    If ElementID = xlSeries Or ElementID = xlDataLabel Then

    If Arg2 > 0 Then

    ' Extract x value from array of x values
    myX = WorksheetFunction.Index _
    (.SeriesCollection(Arg1).XValues, Arg2)
    ' Extract y value from array of y values
    myY = WorksheetFunction.Match _
    (myX, Worksheets("Data").Columns("A"), 0)

    ' Display message box with point information
    MsgBox "Point " & Arg2 & vbCrLf _
    & " - " & Worksheets("Data").Cells(myY, "H").Value

    End If
    End If
    End With
    End Sub[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks for the response XLD, however when I run it I get an error
    "Run-Time Error 13", Type Mismatch, and when i press debug it highlights these two rows of code

    myY = WorksheetFunction.Match _
    (myX, Worksheets("Data").Columns("A"), 0)

    Any ideas?

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    It appears to me that you need to change this line:
    [vba]
    MsgBox "Point " & Arg2 & vbCrLf _
    & " - " & Worksheets("Data").Range("H17:H50").Cells(Arg2, 1).Value [/vba]

    to this:
    [vba]
    MsgBox "Point " & Arg2 & vbCrLf _
    & " - " & myX [/vba]
    Be as you wish to seem

  5. #5
    Thanks Aflatoon,

    almost, but not quite.

    It does now change to reflect the questions displayed, but it doesnt show the actual question any longer, it justs Question 8 (from column A?), not the actual question text (from column H)

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry about that, I tested in XL2007 and it works fine there. In XL2003 it bombs.

    Try this

    [vba]

    Private Sub Chart_MouseUp(ByVal Button As Long, ByVal Shift As Long, _
    ByVal x As Long, ByVal y As Long)
    Dim ElementID As Long, Arg1 As Long, Arg2 As Long
    Dim myX As Variant, myY As Double
    Dim tmp As String

    With ActiveChart

    ' Pass x & y, return ElementID and Args
    .GetChartElement x, y, ElementID, Arg1, Arg2

    ' Did we click over a point or data label?
    If ElementID = xlSeries Or ElementID = xlDataLabel Then

    If Arg2 > 0 Then

    ' Extract x value from array of x values
    myX = WorksheetFunction.Index _
    (.SeriesCollection(Arg1).XValues, Arg2)
    ' Extract y value from array of y values
    Worksheets("Data").Activate
    myY = WorksheetFunction.Match _
    (myX, Worksheets("Data").Columns("A"), 0)

    ' Display message box with point information
    MsgBox "Point " & Arg2 & vbCrLf _
    & " - " & Worksheets("Data").Cells(myY, "H").Value

    End If
    End If
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    My apologies - I did not look at the data closely enough. You merely need to alter xld's code to use:
    [vba] myY = WorksheetFunction.Match _
    (myX, Worksheets("Data").Range("A:A"), 0)
    [/vba]
    Be as you wish to seem

  8. #8
    Thanks very much guys, that works great .

    As always the help you all give is brilliant, and is so gratefully recieved.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •