Results 1 to 5 of 5

Thread: onchange + option button

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    3

    onchange + option button

    Hello!

    I have used in VBA (excel 2000) 3 option buttons, which are grouped. Currently, one of them is clicked and the data connected to it are correct.
    The problem is that if I make a change in a cell, the data are NOT re-calculated. I have to click another option button, and then re-click the one I want and only then the data become correct again.

    So the point is: Is there a way to make the option button to re-calculate the data, whenever ANY change occurs in the current sheet? Something like "onchange"...

    Please help !!
    Thanks!!

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

    Re: onchange + option button

    Welcome to the Forums.

    I would need to see some code to be able to optimize it because depending how your calculations are I could
    suggest a couple of different methods.
    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

    Thread Starter
    New Member
    Join Date
    Mar 2005
    Posts
    3

    Re: onchange + option button

    Well, there are three OptionButtons which call the Sub fetchData. This Sub copies the values of 5 cells to other 5 cells. Its pameters are four. The first two indicate the cells (by row+column), where the data must be pasted. The other two parameters indicate the cells (by row+column again) from where the data are copied.

    What I want is to copy and paste the data whenever a change occurs. If I make a change to the cells from where the data are copied, no change occurs to the cells where the data are pasted. It works only if I re-click the button.

    Thanks a lot!


    CODE:

    Private Sub fetchData(toRow, toCol, fromRow, fromCol)
    toRow = Range("rev1").Row + toRow
    toCol = Range("rev1").Column + toCol

    For i = 0 To 4
    Cells(toRow + i, toCol) = Cells(fromRow, fromCol + i)
    Next i

    End Sub

    Private Sub OptionButton1_Click()
    Call fetchData(0, 0, Range("bas1").Row, Range("bas1").Column)
    End Sub

    Private Sub OptionButton2_Click()
    Call fetchData(0, 0, Range("six1").Row, Range("six1").Column)
    End Sub

    Private Sub OptionButton3_Click()
    Call fetchData(0, 0, Range("phon1").Row, Range("phon1").Column)
    End Sub

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

    Re: onchange + option button

    Looking through the code I would leave the option buttons code since you want to see an update whenever you
    click a different button. I would make use of the SelectionChange event behind the Sheet. This event fires
    everytime a different cell gets the focus. So if you enter a new value in a cell and press enter the focus automatically
    changes to the cell below, thus in effect firing the SelectionChange event and in there you can call your fetchData
    procedure. Note it contains a target range object that may come in handy.
    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
    New Member
    Join Date
    Mar 2005
    Posts
    3

    Re: onchange + option button

    Thanks a lot!
    It works!

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