Consulting

Results 1 to 3 of 3

Thread: max value of another textbox

  1. #1

    max value of another textbox

    If I enter $42.87 in textbox1, how can I restrict the value in textbox 2 to not exceed the vaue in textbox1?

    Thank you

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Perhaps something like

    [VBA]Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    With TextBox1
    .Text = Format(NumFromCur(.Text), "$#.00")
    If NumFromCur(.Text) < NumFromCur(TextBox2.Text) Then
    TextBox2.Text = vbNullString
    End If
    End With
    End Sub

    Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    With TextBox2
    .Text = Format(NumFromCur(.Text), "$#.00")
    Cancel = NumFromCur(TextBox1.Text) < NumFromCur(TextBox2.Text)
    If Cancel Then
    MsgBox "TextBox2 must not exceed TextBox1"
    .SelStart = 0
    .SelLength = Len(.Text)
    End If
    End With
    End Sub

    Function NumFromCur(aString As String) As Double
    NumFromCur = Val(Replace(aString, "$", vbNullString))
    End Function
    [/VBA]

  3. #3
    Thank you Mike. Works like a charm!

Posting Permissions

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