"If" Statement Not Running Even When Statement is True

ashperson95

New Member
Joined
May 16, 2014
Messages
36
Hello all,

I never thought I would have so much trouble trying to do simple user error checking. I have a little block of code that asks for a user input, and then it checks to see if the input is valid (numeric and between 1 and 7). The if statement runs when the entry is not numeric, but if the value is greater than 7 or less than 1, the if statement does not run.

Code:
Dim hoursWeek As Variant

hoursWeek = Application.InputBox("How many days a week does this client operate their lights?", "Days Per Week")


If hoursWeek > 7 Or hoursWeek < 1 Or (Not IsNumeric(hoursWeek)) Then
Do Until IsNumeric(hoursWeek) And (1 <= hoursWeek <= 7)
hoursWeek = Application.InputBox("Sorry, that's not a valid entry. How many days a week does this client operate their lights?", "Days Per Week")
Loop
End If

I have tried changing the hoursWeek variable in the If statement to Val(hoursWeek.Text), CInt(hoursWeek), and hoursWeek.Value. I've also tried declaring hoursWeek as a String and using CInt(hoursWeek) in the If statement, but none of these have worked.

What is it that I'm doing wrong?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The input box you're using return a string. Use the type "1" to only allow numeric entries.

Code:
    Dim hoursWeek As Variant

    hoursWeek = Application.InputBox("How many days a week does this client operate their lights?", "Days Per Week", Type:=1)

    If hoursWeek > 7 Or hoursWeek < 1 Then
        Do Until hoursWeek <= 7 And hoursWeek >= 1
            hoursWeek = Application.InputBox("Sorry, that's not a valid entry. How many days a week does this client operate their lights?", "Days Per Week", Type:=1)
        Loop
    End If
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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