actuallife
actuallife

Reputation: 89

How to make sure a number entered in an InputBox is less than another number entered in an InputBox in VBA?

I am having the user enter in a lower bound and upper bound that I then use to filter rows. For example, if they type 4 for the lower bound, and 8 for the upper bound, I then use those numbers to filter for rows that contain a 4, 5, 6, 7, or 8 in the first cell.

My code (which I will show below) ensures that the user can only enter a number (by specifying type as 1), but I want to make sure that the second number they type in is always bigger than the first. Not sure if this is possible with how I'm doing it currently with two different inputboxes, but I'm not sure how else to do it as this is only my 4th day coding in VBA. I would also like to make sure that the numbers they enter in for both the lower and upper bound are greater than 0.

Here is my code for the InputBoxes:

LowerBound = Application.InputBox("Please enter the number of the first line item:", "First Line Item Number", , , , , , 1)
UpperBound = Application.InputBox("Please enter the number of the last line item:", "Last Line Item Number", , , , , , 1)

Any help is appreciated!

Upvotes: 1

Views: 312

Answers (1)

Gary's Student
Gary's Student

Reputation: 96753

This will repeat the questions until two valid values have been entered:

Sub LimitCheck()
    Dim Repeat As Boolean
    Repeat = True
    While Repeat
        lowerbound = Application.InputBox("Please enter the number of the first line item:", "First Line Item Number", , , , , , 1)
        upperbound = Application.InputBox("Please enter the number of the last line item:", "Last Line Item Number", , , , , , 1)
        If lowerbound < upperbound And lowerbound > 0 Then
            Repeat = False
        End If
    Wend
    MsgBox "Done"
End Sub

If upperbound can be equal to lowerbound the use:

If lowerbound <= upperbound And lowerbound > 0 Then

Upvotes: 2

Related Questions