Calculating multiple text boxes on a user form

kevinc1973

New Member
Hello all,
I am calculating multiple text boxes on a user form and the code i am using seems to be working out okay. The problem i'm having is if I tab into the text boxes that have the code to multiply and divide are left blank are giving me a error message. Some days I may leave those text boxes blank because of no data. How do I go around this so those text boxes stay blank.
HTML:
Private Sub textbox4_enter()
TextBox4.value = (TextBox2.value - TextBox3.value)
End Sub

Private Sub TextBox8_Enter()
TextBox8.value = (TextBox2.value - TextBox7.value)
End Sub

Private Sub TextBox9_Enter()
TextBox9.value = ((TextBox8.value * 100)) / (TextBox4.value)
End Sub]
If I don't enter into the text boxes it works but i can't guarantee no one else will not tab into the text boxes and get the error message, as there will be multiple people using the user form. thanks for any help
 

Yongle

Well-known Member
One method
- prevent calculation if either textbox is empty

Use se something like this as first line of TextBox9_Enter
Code:
If Len(TextBox4.Text) = 0 Or Len(TextBox8.Text) = 0 Then Exit Sub
 

dmt32

Well-known Member
try following update to your codes

Code:
Private Sub textbox4_enter()
    TextBox4.Value = (Val(TextBox2.Value)) - Val(TextBox3.Value)
End Sub


Private Sub TextBox8_Enter()
    TextBox8.Value = (Val(TextBox2.Value)) - Val(TextBox7.Value)
End Sub


Private Sub TextBox9_Enter()
    TextBox9.Value = (Val(TextBox8.Value) * 100) / Val(TextBox4.Value)
End Sub
The VAL function stops reading a string at the first character that it can't recognize as part of a number so should return 0 when you TAB around empty textboxes

Dave
 
Last edited:

jmacleary

Well-known Member
try following update to your codes

Code:
Private Sub textbox4_enter()
    TextBox4.Value = (Val(TextBox2.Value)) - Val(TextBox3.Value)
End Sub


Private Sub TextBox8_Enter()
    TextBox8.Value = (Val(TextBox2.Value)) - Val(TextBox7.Value)
End Sub


Private Sub TextBox9_Enter()
    TextBox9.Value = (Val(TextBox8.Value) * 100) / Val(TextBox4.Value)
End Sub
The VAL function stops reading a string at the first character that it can't recognize as part of a number so should return 0 when you TAB around empty textboxes

Dave
This solution will still error if TextBox9 is entered when TextBox4 is empty (you would get a division by zero error).

So, for TextBox9 do something like:
Code:
Private Sub TextBox9_Enter()
    if Val(TextBox4.Value)<>0 then
       TextBox9.Value = (Val(TextBox8.Value) * 100) / Val(TextBox4.Value)
    endif
End Sub
 
Last edited:

dmt32

Well-known Member
This solution will still error if TextBox9 is entered when TextBox4 is empty (you would get a division by zero error).

So, for TextBox9 do something like:
Code:
Private Sub TextBox9_Enter()
    if Val(TextBox4.Value)<>0 then
       TextBox9.Value = (Val(TextBox8.Value) * 100) / Val(TextBox4.Value)
    endif
End Sub
Silly me - one of my senior moments;)

Dave
 

kevinc1973

New Member
Thanks everyone,
I will try the codes when I'm back at work. I also tried
Code:
Textbox9.dblclick
That let me tab through with no data entered I just had to double click in the box to have it calculate.
 

kevinc1973

New Member
Thank you guys for the help it worked perfectly just by entering the Val in the code. Once again you guys are awesome thank you
Kevin
 

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Macro to copy values across rows and transposing them and add the user id
    [FONT=Times New Roman][SIZE=3][COLOR=#000000][/COLOR][/SIZE][/FONT][FONT=Calibri][SIZE=3][COLOR=#000000]Hi,[/COLOR][/SIZE][/FONT] [FONT=Times New...
Top