+ Reply to Thread
Results 1 to 4 of 4

3 dependent textboxes in a userform

Hybrid View

Leandrial 3 dependent textboxes in a... 12-16-2012, 07:30 AM
alansidman Re: 3 dependent textboxes in... 12-16-2012, 07:57 AM
AlphaFrog Re: 3 dependent textboxes in... 12-16-2012, 12:53 PM
mikerickson Re: 3 dependent textboxes in... 12-16-2012, 01:24 PM
  1. #1
    Registered User
    Join Date
    11-24-2012
    Location
    Denmark
    MS-Off Ver
    Excel 2010
    Posts
    15

    3 dependent textboxes in a userform

    Hi

    I am trying to create 3 textboxes in a userform, where the value in the boxes are inter-dependent (dont know if thats even a word...)

    I have made functions, so that when i enter a value in any of the 3 boxes, the code can calculate the value for the other 2...

    Problem is, that i kinda end up in a bad loop...

    I use the "afterupdate" event, to trigger the calculation of the 2 missing values... and assign that calculatet value to the 2 missing values... and that works fine.. but problem is that the assigning of the calculated values to the textboxes in it self triggers an "afterupdate"-event.. and a form of bad loop happens... cuz then the code try to calculate "back"

    Is there a way to get the code to reckognize when im manually entering a value (and for that trigger an afterupdate event, recalculating the value for the other 2 textboxes) or when a new value is enteret by the code (based on a function) and therefor not trigger the afterupdate event...??


    Kind regards

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,704

    Re: 3 dependent textboxes in a userform

    please disregard my post.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,652

    Re: 3 dependent textboxes in a userform

    Toggle the Application.EnabelEvents property to prevent triggering another event procedure.

        Application.EnableEvents = False
            'Update the other two interdependent textboxes here
        Application.EnableEvents = True

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

    Re: 3 dependent textboxes in a userform

    EnableEvents doesn't effect userform events, but you can get the same effect with a module wide boolean variable

    ' in userforms code module
    
    Dim ufEventsDisabled As Boolean
    
    Private Sub TextBox1_AfterUpdate()
        If ufEventsDisabled Then Exit Sub
        
        ufEventsDisabled = True
        TextBox3.Text = CStr(Val(TextBox1.Text) + Val(TextBox2.Text))
        ufEventsDisabled = False
    End Sub
    
    Private Sub TextBox2_AfterUpdate()
        If ufEventsDisabled Then Exit Sub
        
        ufEventsDisabled = True
        TextBox3.Text = CStr(Val(TextBox1.Text) + Val(TextBox2.Text))
        ufEventsDisabled = False
    End Sub
    
    Private Sub TextBox3_AfterUpdate()
        If ufEventsDisabled Then Exit Sub
        
        ufEventsDisabled = True
        TextBox2.Text = CStr(Val(TextBox3.Text) - Val(TextBox1.Text))
        ufEventsDisabled = False
    End Sub
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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