Code starts in wrong place

Janet052

New Member
Joined
Apr 29, 2011
Messages
5
I am using code from J.E. McGimpsey's site, which works great. I have a problem with the code starting on the wrong line, but cannot figure out how to correct it. This is the table before the code is run:

column A colum B
Name Random Number Assignment
Janet
Janet
Mark
Sam
Sam
Sam


Tabel after code is run: The title in column B gets overwritten with a random number and the last row is blank

column A colum B
Name 3
Janet 6
Janet 2
Mark 8
Sam 7
Sam 4
Sam

This is the code:
Sub RandomNumber()
Dim Lowest As Long
Dim Highest As Long
Dim x As Long
Dim y As Long
Dim Total As Long
Dim ChoiceTemp As Long
Dim Choice() As Long
Dim Repeat As Boolean

Application.ScreenUpdating = False
Lowest = 1
Highest = InputBox("what is the highest number?")
Total = InputBox("How many combinations do you want")

ReDim Choice(1 To Total)

For x = 1 To Total
a:
Randomize
Repeat = False
ChoiceTemp = Int((Highest + 1 - Lowest) * Rnd + Lowest)

For y = 1 To Total
If Choice(y) = ChoiceTemp Then
Repeat = True
Else
End If
Next y
If Repeat = True Then
GoTo a:
Else
Choice(x) = ChoiceTemp
End If
Next x
For x = 1 To Total

Sheets("Sheet1").Range("B" & x).Value = Choice(x)


Next

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
The random number generator should enter its first number starting in "B2" and end on "B7" with the last name encountered in column A. The High would be 6 in this example with 6 combinations.
 
Upvote 0
I did do that at one point and it solved half the problem. The other problem is that the last name in Col A does not have a number assigned to it. I had resolved simply to remember to add "1" to the number of names that were in Col A. This fixes the problem, but I am not sure why, or if it is the best way to take care of the "blank"
 
Upvote 0
Sorry, I am not very good at this. A novice at best.

I have values in A2:A7 that need a random number assigned in B2:B7. Since there are 6 values from A2:A7, I use the number 6 as my input in the message box and 6 for the number of combinations that I am asking the random number generator to produce in B2:B7.

What happens is the first number generated is put in B1, which the solution of " For x = 2 To Total" resolved. Random numbers are entered starting in cell B2 through B6.

B7 is left blank with no random number.
 
Upvote 0
Hello

Try this.

change this line back to this.

Code:
For x = 1 To Total

And change the line starting with Sheets to.

Code:
Sheets("Sheet1").Range("B" & x).Offset(1).Value = Choice(x)
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,693
Members
452,938
Latest member
babeneker

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