Difference between False and '0'

DeepUnderground
07-19-2010, 02:08 PM
I have the following VBA Code:

Option Explicit

Sub mySheetFormat()
Dim i As Variant
Dim MinSheets As Integer, MaxSheets As Integer
Dim myTitle As String, myPrompt As String
Dim myFlag As Boolean

MinSheets = 1
MaxSheets = 5
myTitle = "# of Sheets"
myPrompt = "Input the Number of Worksheets."

Do
myFlag = False
On Error Resume Next
i = Application.InputBox(Prompt:=myPrompt, Title:=myTitle)

If i = False Then
Exit Do
ElseIf Not (i) = CInt(i) Then
myFlag = True
End If

myTitle = "Warning!!!"
myPrompt = "Workheets Must Be An Integer Between " & Str(MinSheets) & " and " & Str(MaxSheets) & "."
Loop Until i >= MinSheets And i <= MaxSheets And myFlag = False

End Sub

It works fine, except when the user inputs 0 into the input box. The code things that 0=false and it exits the loop. If the user inputs 0 and selects "ok", it should go back into the do loop. Anyone have any ideas?


Please use the code tags when posting code. Thank you.

ZKat
07-19-2010, 02:39 PM
Hi,

I may be reading this wrong, but if the user enters 0, would you not want the code to exit ?

Will there ever be a time when the user would enter 0 ?

I just scanned this code, so there must be a another part that you did not post.

DeepUnderground
07-19-2010, 03:27 PM
If the user enters 0 and presses ok the message will come up "Workheets Must Be An Integer Between 1 and 5". Therefore it would go back into the loop until the user enters either 1, 2, 3, 4, 5 or selects cancel.

Bob Phillips
07-19-2010, 03:33 PM
No, he is treating 0 as an invalid number, so it shouldn't exit. He only wanst Cancel to exit.

DeepUnderground
07-19-2010, 03:42 PM
I got it:

If i = False And Application.IsLogical(i) Then
Exit Do
ElseIf Not (i) = CInt(i) Then
myFlag = True
End If

It isn't pretty but I added, "Application.IsLogical(i)" to the if statement inside the do loop. If anyone has a cleaner way to handle this let me know.

ZKat
07-19-2010, 03:49 PM
OK then take out the exit Do.

But, l see no code for the Cancel. After the user enters the variable sheet number what happens?

DeepUnderground
07-19-2010, 04:14 PM
If i = False And Application.IsLogical(i) Then
Exit Do
ElseIf Not (i) = CInt(i) Then
myFlag = True
End If

when the i = false, the cancel is selected, and since Application.IsLogical(i) we know it is either false or true. If zero is selected then it just goes back into the loop.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum