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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Nimrod said:
I'm on the road for the next few day's but have checked in. In response to Mr. Nobs concerns about entry of dates , I don't see how dates are a concern as the Type:= 1, prevents anything but numbers being entered into the input box. This "feature" of the input box has been explained in my previous postings ... OK ? :confused:

Not OK.

On my computer, Excel recognizes a date as a number and therefore your code accepts date inputs.

Have you actually tried it ?
 
Upvote 0
Mr Nob:

I will be off "the road" on friday and will take a look at your concerns at that time. Currently I'm at a Comdex convention using the "Cyber Centers" computers that do not have Excel. Please indicate exactly what you typing in and I will certainly address your concerns before the end of the week.

Have a nice day :cool:
 
Upvote 0
Nimrod said:
Mr Nob:

I will be off "the road" on friday and will take a look at your concerns at that time. Currently I'm at a Comdex convention using the "Cyber Centers" computers that do not have Excel. Please indicate exactly what you typing in and I will certainly address your concerns before the end of the week.

Have a nice day :cool:

No need to be be such a patronising ****.

The problem is not mine. I fully understand how Excel treats dates and was merely trying to help you to improve your knowledge of Excel.

Try testing your code properly and looking up the Help files so you can establish for yourself the shortcomings of your suggested macro.
 
Upvote 0
Hello Mr Nob
Hmmmm….. I didn’t realize I was a “patronising s**t”. :oops: As I explained in my earlier posts I was “on the road” and had no excel to test “a date” to see what you were talking about. I feel I was quite polite to you in my posts and am surprised by the feedback I have received from you.
Now that I am back home and can test dates I see that the Type:=1 does allow dates of certain formats to be seen as numbers. So thanks for pointing that out…. I just wish you could have done that with a little more … tack . :wink:
Anyway I’ve looked at your code and found the suggested modifications that test for dates to be a good idea. However I’m not understanding the need to slow the procedure down by calling the “val” function several times ? As the following code seems to work just fine.
I certainly appreciate your input on the code though I hope you can cut me a little slack with the “attitude”. Thx.
(y)

Public Sub AskForNumV5()
Do
Response = Application.InputBox(agn & "Please Enter Integer Between 1 and 100", "Enter Number")
If Response = False Then Exit Sub
agn = "Sorry the entry... " & Response & " ..is invalid" & vbCrLf
Loop Until Response > 0 And Response < 101 And Not IsDate(Response)
MsgBox "You successfully entered " & Val(Response)
End Sub
 
Upvote 0
Nimrod said:
I’m not understanding the need to slow the procedure down by calling the “val” function several times ?

Hmmm .... picky as well as patronising.

It does appear that using Val is not necessary and that your line of code :
Loop Until Response > 0 And Response < 101 And Not IsDate(Response)
does the same as :
Loop Until Val(response) > 0 And Val(response) < 101 And response = Int(Val(response)) And Not IsDate(response).

However, using Timer I recorded no difference in the run time of each of alternative.

This means that if there is any difference in the run times and there is in fact a "slow down" of the procedure, it must be less than one hundreth of a second.

(Also, "several times" could be considered by some to be an unusual term to describe three times.)
 
Upvote 0
Mr Nob:

I enjoy helping people and that's why I come here. I've tried to understand and help you out. In return I've find you rude . Since I don't enjoy my communication with you it only makes sense that I avoid it .

Your an intelligent person though I do hope you learn some better communicatin skills so that you can fulfil your full potential.

Good Luck ... an I'm not being sarcastic. (y)
 
Upvote 0
Nimrod said:
Mr Nob:

I enjoy helping people and that's why I come here. I've tried to understand and help you out. In return I've find you rude . Since I don't enjoy my communication with you it only makes sense that I avoid it .

Your an intelligent person though I do hope you learn some better communicatin skills so that you can fulfil your full potential.

Good Luck ... an I'm not being sarcastic. (y)


"I've tried to understand and help you out .... I do hope you learn some better communicatin skills so that you can fulfil your full potential"

Absolutely wonderful!
Exactly the sort of patronising crap I was referring to.
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

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