Results 1 to 6 of 6

Thread: *Resolved* Excel & "On SelectionChange" question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Resolved *Resolved* Excel & "On SelectionChange" question

    I have a query I am hoping you can help me with.

    I have an Excel chart and I want the user to be able to change to range of the x-axis to any number between 2 and 100. They do this simply by entering a number in a cell and the chart x-axis should automatically update so that the Max. Number for the x-axis equals the user input value.

    For the sake of argument suppose that the cell that the user needs to enter the number in is cell A1.

    I already have the code that changes the x-axis, which is as follows:

    Code:
    With ActiveSheet
            .ChartObjects("Chart 1").Chart.Axes(xlCategory).MaximumScale = Range("A1")
    End With
    The problem is I don't know how to get the spreadsheet to execute this piece of code. I thought that the SelectionChange event might be the one I need but I can't get it to work.

    Also, ideally I only want the spreadsheet to do anything if cell A1 only is changed i.e. it doesn't need to update the chart x-axis if some other cell is changed.

    So ideally I need code that says:

    Code:
    If Cell A1 is changed then make the x-axis of Chart 1 equal the new value
    Thanks in advance
    -Rob
    Last edited by TheRobster; Jul 10th, 2005 at 04:15 PM.
    http://www.sudsolutions.com

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel & "On SelectionChange" question

    Either should do it. Your choice .
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.     If Target.Address = "$A$1" Then
    3.         'Update chart
    4.     End If
    5. End Sub
    6.  
    7. 'Or
    8. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    9.     If Target.Address = "$A$1" Then
    10.         'Update chart
    11.     End If
    12. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Excel & "On SelectionChange" question

    Note for what RobDog said. If I remember correctly the _Change sub will only fire after the user exits the cell that is being changed.

    To elaborate... say you type 50 in the cel and leave the cursor sitting there.. it wont fire the event, but if you move the cursor to another cell then the event fires if it was changed from its previous value.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Excel & "On SelectionChange" question

    Correct and good description of the event.

    You could also add a menu or toolbar button to do your chart refresh if you want to also.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: Excel & "On SelectionChange" question

    Cheers for the replies guys.

    Quote Originally Posted by RobDog888
    You could also add a menu or toolbar button to do your chart refresh if you want to also.
    No, the methods you outlined earlier will be fine. Before I posted my query on here I was actually thinking about using a button labelled "Update" or something similar and binding this to a macro which would update the chart x-axis but your solution is more elegant.

    -Rob
    http://www.sudsolutions.com

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: *Resolved* Excel & "On SelectionChange" question

    As always we are glad to have helped.

    Btw, did you see that we have a new way to do a simple "[RESOLVED]"? Its from the Thread Tools menu > Mark Thread Resolved.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width