FOR NEXT LOOP QUESTION

dabluebery

Board Regular
Joined
Sep 22, 2003
Messages
83
Hey,

I'm in a VBA class, and I have a simple HW assignment that I don't quite understand. Here's the question;

Write a sub that asks for a product index from 1 to 100. Embed this within a Do loop so that the user keeps being asked until he enters an integer from 1 to 100. (use a For loop for checking)

I don't understand how I would use a for loop to check if the number is an integer. I understand the first part of the question, and already have a do loop checking if the value entered is numeric. If not, it prompts an input box until you do. Any insight is appreciated.

Rob
 
AskForNumV2() allows a string entry of False

You can correct for this by using:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> AskForNumV3()
<SPAN style="color:darkblue">Dim</SPAN> response <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Variant</SPAN>
Do: response = Application.InputBox(agn & _
    "Please Enter <SPAN style="color:darkblue">Integer</SPAN> Between 1 and 100", "Enter Number")
<SPAN style="color:darkblue">If</SPAN> response = <SPAN style="color:darkblue">False</SPAN> And <SPAN style="color:darkblue">Not</SPAN> TypeName(response) = "String" <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
agn = "Sorry the entry... " & response & " ..is invalid" & vbNewLine
<SPAN style="color:darkblue">Loop</SPAN> <SPAN style="color:darkblue">Until</SPAN> Val(response) > 0 And Val(response) < 101 And _
    response = Val(response) \ 1 And <SPAN style="color:darkblue">Not</SPAN> IsDate(response)
MsgBox "You successfully entered " & response
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>

Keep the Val() functions to guard your procedure against string entries.

Also, can we mind the tone and nature of our discussions in this forum somewhat? Go fight in the lounge. :)
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
NateO said:
AskForNumV2() allows a string entry of False

You can correct for this by using:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> AskForNumV3()
<SPAN style="color:darkblue">Dim</SPAN> response <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Variant</SPAN>
Do: response = Application.InputBox(agn & _
    "Please Enter <SPAN style="color:darkblue">Integer</SPAN> Between 1 and 100", "Enter Number")
<SPAN style="color:darkblue">If</SPAN> response = <SPAN style="color:darkblue">False</SPAN> And <SPAN style="color:darkblue">Not</SPAN> TypeName(response) = "String" <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
agn = "Sorry the entry... " & response & " ..is invalid" & vbNewLine
<SPAN style="color:darkblue">Loop</SPAN> <SPAN style="color:darkblue">Until</SPAN> Val(response) > 0 And Val(response) < 101 And _
    response = Val(response) \ 1 And <SPAN style="color:darkblue">Not</SPAN> IsDate(response)
MsgBox "You successfully entered " & response
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>

Keep the Val() functions to guard your procedure against string entries.

Also, can we mind the tone and nature of our discussions in this forum somewhat? Go fight in the lounge. :)

That's the same as I posted earlier in this thread.
 
Upvote 0
OK (but can't think why anyone would consider it necessary).
If the point is to validate, one might as well validate. Why leave an exception?

Otherwise, save yourself some memory and use a string variable rather than a variant variable.
 
Upvote 0
NateO said:
Otherwise, save yourself some memory and use a string variable rather than a variant variable.

How much memory would be saved?


Edited by NPO: Fixed Quote tags.
 
Upvote 0
Good Evening (here at least :p ),

Ponsy Nob. said:
Would certainly want to make every effort and to take all possible steps to avoid unnecessarily using 6 bytes of memory.
Feeling argumentative? :LOL:

Ok, I'll bight. :)

First you want to trap for dates, but not a "false" string. Now you want to needlessly waste memory on your respective, subjective 'I don't care' non-validation scenario...

Why would one go out of their way to waste any amount of memory when it comes down to a very simple syntax choice?

There's no harm in getting into good coding habits, declaring variant variables [when you don't need to], or creating them from a lack of declaration is not one of these.

Microsoft said:
The disadvantage is that variant variables require at least 16 bytes of memory, which can be significant in large procedures or complex modules.
 
Upvote 0
Why would you think that I'm being argumentative?

Did I not agree with you by posting :-
"Would certainly want to make every effort and to take all possible steps to avoid unnecessarily using 6 bytes of memory." ?

Or perhaps you really think that it's not worth making an effort to avoid using 6 bytes of memory?

The Microsoft statement ("The disadvantage is that variant variables require at least 16 bytes of memory, which can be significant in large procedures or complex modules.") is not relevant in that the subject procedure is neither large nor complex.
(Also, assertions by Microsoft are not necessarily always reliable.)

Your statement "First you want to trap for dates" is not quite correct.
In fact, to satisfy the requirements of the OP I don't think it's necessary to do so, and Nimrod's suggested macro is a practical solution.
The only reason I mentioned dates in the first place was to correct the wrong information being posted about what input is allowed by specifying "Type1" input and to make it known that "Type1" accepts dates.

Also, can we mind the tone and nature of our discussions in this forum somewhat? Go argue in the lounge.
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,700
Members
449,464
Latest member
againofsoul

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