Loop question

karl_burns

New Member
Joined
Jul 10, 2007
Messages
19
Back with a loop question.

I would like the user to tell me how many times to repeat data gathering.
For each line of data, I will need to ask for two pieces of information.

I have tried to run some "Do Until" and "Do When" with an growing integer, but I just cannot seem to get it right.

Please help.

Regards,
Karl
 

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.

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
If you want the user to specify the number of loops, this is a brief, untested example:

Code:
Dim NumTimes As Long, X As Long

NumTimes = InputBox("How many times to repeat?")
If NumTimes = 0 Then Exit Sub

For X = 1 To NumTimes
    'code to loop goes here
Next X

MsgBox "Looping finished!"
 
Upvote 0

karl_burns

New Member
Joined
Jul 10, 2007
Messages
19
WOW Thanks! My only other question would be, if the user replies 3, how do I code the loop to ask three times (ABOVE) and write the 3 pieces of given data in three different rows?

Thanks again for the quick response.

Karl
 
Upvote 0

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
The 'For X = 1 To NumTimes' is the loop.

It increments the value of X by 1 until the value of 'NumTimes' is reached.

So for example, if X were referring to a row number:
Code:
For X = 1 To NumTimes
    MsgBox Cells(X, "A").Value
    MsgBox Range("A" & X).Value
Next X

You haven't really said what you're looping, though. Are the 3 different rows the same rows every time or does it vary? If it varies, what is the criteria? What rows?

It may simply be easier to figure out what you're doing/wanting if you could post the code you currently have...
 
Upvote 0

karl_burns

New Member
Joined
Jul 10, 2007
Messages
19
I just hard coded it bu copying and pasting since my largest number can only be 10.
But here's my code to give you a better idea:

CODE:

If NumTimes = 5 Then
Range("c11").Value = InputBox("What is the name of the country?")
Range("d11").Value = InputBox("How many total personnell??")
Range("c12").Value = InputBox("What is the name of the country?")
Range("d12").Value = InputBox("How many total personnell??")
Range("c13").Value = InputBox("What is the name of the country?")
Range("d13").Value = InputBox("How many total personnell??")
Range("c14").Value = InputBox("What is the name of the country?")
Range("d14").Value = InputBox("How many total personnell??")
Range("c15").Value = InputBox("What is the name of the country?")
Range("d15").Value = InputBox("How many total personnell??")
End If


Thanks Krystal!
Karl
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
try
Code:
    If NumTimes = 5 Then
         For i = 11 To 15
              Range("c" & i).Value = InputBox("What is the name of the country?")
              Range("d" & i).Value = InputBox("How many total personnell??")
         Next
    End If
 
Upvote 0

Forum statistics

Threads
1,191,188
Messages
5,985,195
Members
439,947
Latest member
fabiannic

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
Top