+ Reply to Thread
Results 1 to 10 of 10

Toggle option buttons to switch numbers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Toggle option buttons to switch numbers

    Hi,
    I need help to toggle between % or $ down payment. I can input % then calculate the % of a loan.
    When I click $ option button, I want the $value to switch in the first row, % in second row.

    Thanks
    Attached Files Attached Files
    Last edited by Rocky2013; 03-09-2015 at 09:30 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,991

    Re: Toggle option buttons to switch numbers

    See attached
    Attached Files Attached Files
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: Toggle option buttons to switch numbers

    The result is not is not exactly what I need.


    1. If I click % option button,
    D7 I enter 5.00%,
    D9 will show a calculated value $1000.

    2. If I click $ option button,
    D7 will automatically change from % value/unit to $1,000,
    D9 will atomatically change to 5.00%.

    The steps are the same if $ button is first picked, $1000 will type in D7 , 5% will show in D9.
    Click % button, then D7 will show 5.00%, D9 will show $1,000. Basicly D7 and D9 incliding
    the unit symbols switch back and fore.

    Hopefully I explain it right.
    Last edited by Rocky2013; 03-09-2015 at 08:06 AM.

  4. #4
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Toggle option buttons to switch numbers

    Hello
    Check attached
    I am new to vba
    Sorry if this was not what you required...
    Attached Files Attached Files
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "I don't get things easily, so please be precise and elaborate"

    If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
    If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.

    Sourabh

  5. #5
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: Toggle option buttons to switch numbers

    Thanks, it works but there is a pop-up message ....
    Run-time error '1004';
    Method 'Range' of objects'_Globe' failede.

    If you are new in VBA, I am just an infant. Please advise if you can eliminate the pop-up
    or welcome who may offer help.

    Thanks all!
    Last edited by Rocky2013; 03-09-2015 at 08:37 PM.

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Toggle option buttons to switch numbers

    Here's my effort, you'll have to change the locations for rngBaseValue, modeCell and otherCell to match your worksheet.
    I've reworked the display a bit in my attachment.

    Sub test()
        Dim CurrentMode As String
        Dim NewMode As String
        Dim rngBaseValue As Range
        Dim modeCell As Range, otherCell As Range
        Dim Dollars As Double, Percent As Double
        Const DollarFormat As String = "$ #,##0"
        Const PctFormat As String = "#.00 %"
        
        Set rngBaseValue = Sheet1.Range("A1")
        Set modeCell = Sheet1.Range("A3")
        Set otherCell = Sheet1.Range("A5")
        
        If modeCell.NumberFormat = DollarFormat Then
            CurrentMode = "dollar"
        Else
            CurrentMode = "percent"
        End If
        
        If ActiveSheet.Shapes("Option Button 1").ControlFormat.Value = xlOn Then
            NewMode = "percent"
        Else
            NewMode = "dollar"
        End If
        
        If CurrentMode <> NewMode Then
            If CurrentMode = "dollar" Then
                Dollars = modeCell.Value
                Percent = Dollars / Val(CStr(rngBaseValue.Value))
            Else
                Percent = modeCell.Value
                Dollars = Val(CStr(rngBaseValue.Value))
            End If
                
            If NewMode = "dollar" Then
                With modeCell
                    .NumberFormat = DollarFormat
                    .Value = Dollars
                End With
                With otherCell
                    .NumberFormat = PctFormat
                    .Formula = "=" & modeCell.Address & "/" & rngBaseValue.Address
                End With
                
            Else
                With modeCell
                    .NumberFormat = PctFormat
                    .Value = Percent
                End With
                With otherCell
                    .NumberFormat = DollarFormat
                    .Formula = "=" & modeCell.Address & "*" & rngBaseValue.Address
                End With
            End If
        End If
    End Sub
    Attached Files Attached Files
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: Toggle option buttons to switch numbers

    Hi surabhg98
    Foe some reason, wen I saved the file then reopen, the error message did not pop-up. It works now. Thanks

    Hi Mike,
    Thanks for the quick replay of your effort to help. I have to play around with your code.

    Thanks both again!

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Toggle option buttons to switch numbers

    I just noticed an error.
    Line 32 needs to be changed.
    Sub test()
        Dim CurrentMode As String
        Dim NewMode As String
        Dim rngBaseValue As Range
        Dim modeCell As Range, otherCell As Range
        Dim Dollars As Double, Percent As Double
        Const DollarFormat As String = "$ #,##0"
        Const PctFormat As String = "#.00 %"
        
        Set rngBaseValue = Sheet1.Range("A1")
        Set modeCell = Sheet1.Range("A3")
        Set otherCell = Sheet1.Range("A5")
        
        If modeCell.NumberFormat = DollarFormat Then
            CurrentMode = "dollar"
        Else
            CurrentMode = "percent"
        End If
        
        If ActiveSheet.Shapes("Option Button 1").ControlFormat.Value = xlOn Then
            NewMode = "percent"
        Else
            NewMode = "dollar"
        End If
        
        If CurrentMode <> NewMode Then
            If CurrentMode = "dollar" Then
                Dollars = modeCell.Value
                Percent = Dollars / Val(CStr(rngBaseValue.Value))
            Else
                Percent = modeCell.Value
                Dollars = Val(CStr(rngBaseValue.Value)) * Percent: Rem change this <<<<<<<<<<<<<<<<<<
            End If
                
            If NewMode = "dollar" Then
                With modeCell
                    .NumberFormat = DollarFormat
                    .Value = Dollars
                End With
                With otherCell
                    .NumberFormat = PctFormat
                    .Formula = "=" & modeCell.Address & "/" & rngBaseValue.Address
                End With
                
            Else: Rem newmode = "percent"
                With modeCell
                    .NumberFormat = PctFormat
                    .Value = Percent
                End With
                With otherCell
                    .NumberFormat = DollarFormat
                    .Formula = "=" & modeCell.Address & "*" & rngBaseValue.Address
                End With
            End If
        End If
    End Sub

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Toggle option buttons to switch numbers

    Alternately, you could use the Change event so that there's no need to toggle.

    In the attached, on Sheet2, C1 is the base amount. If the user enters dollars in C3 or percentages in C5, the other will adjust its value.

    ' in sheet's code module
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rngBaseCell As Range
        Dim dollarCell As Range, percentCell As Range
        
        Set rngBaseCell = Range("C1")
        Set dollarCell = Range("C3")
        Set percentCell = Range("C5")
        
        If Not Application.Intersect(Target, rngBaseCell) Is Nothing Then
            Application.EnableEvents = False
            percentCell.Value = Val(CStr(dollarCell.Value)) / Val(CStr(rngBaseCell.Value))
        End If
        
        If Not Application.Intersect(Target, dollarCell) Is Nothing Then
            Application.EnableEvents = False
            percentCell.Value = Val(CStr(dollarCell.Value)) / Val(CStr(rngBaseCell.Value))
        End If
        
        If Not Application.Intersect(Target, percentCell) Is Nothing Then
            Application.EnableEvents = False
            dollarCell.Value = Val(CStr(percentCell.Value)) * Val(CStr(rngBaseCell.Value))
        End If
        
        Application.EnableEvents = True
    End Sub
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    10-03-2012
    Location
    USA
    MS-Off Ver
    2010
    Posts
    496

    Re: Toggle option buttons to switch numbers

    Hi Mike,
    Thanks you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Toggle Button to Clear Option Buttons
    By nwb in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-24-2014, 05:58 PM
  2. [SOLVED] Disabling an Entire Frame with Option Buttons if other Option Buttons are not clicked.
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-20-2012, 04:07 AM
  3. Using control toolbox's option buttons to switch between vlookup tables
    By da_sprite in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2009, 05:45 AM
  4. Linking a cell to a toggle switch
    By Brisbane Rob in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-28-2005, 07:41 PM
  5. Toggle on/off switch for x-y graph
    By Brad in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 08-18-2005, 11:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1