IF statements in VBA

VBAmalta89

Board Regular
Joined
Mar 25, 2011
Messages
116
I need to create an if statement in VBA for the following problem but cant get around to understand how to actually do it:

I have three text boxes in a user form that only accept numbers. Now, I need the total of these three numbers to not exceed 100, ie a prompt is given if 100 is exceeded and the user will be unable to complete the form

how do i code this?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
Sub its_over_9000()
    If Range("a1") + Range("a2") + Range("a3") > 100 Then
        MsgBox "TOO MUCH!!!!!"
    End If
End Sub
 
Upvote 0
Like this?

Code:
If (Val(TextBox1.Value) + Val(TextBox2.Value) + Val(TextBox3.Value)) > 100 Then
    MsgBox "Maximum of 100 exceeded!"
    Exit Sub
End If
 
Upvote 0
Hi there,

Try:

Code:
    Dim txb As Control, temp As Single
    For Each txb In UserForm1.Controls
        If txb <> "" Then
            temp = temp + txb
        End If
    Next txb

    If temp > 100 Then
        MsgBox "Value is over 100: " & temp
        TextBox1.SetFocus
    Else
        doStuff
    End If

This will not count blank textboxes.
 
Upvote 0
Well, I assumed he knew to change that to what he needed. Silly me.

actually, now that I think about it
"For Each txb In UserForm1.Controls"

How do you know it's called UserForm1? and how do you know there aren't other controls than the 3 he's adding?

I'm just giving you a hard time.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,290
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top