I have a question regarding some VBA code I am using to generate random numbers. In a varying range, I want the code to generate a random number, and then check all previous ranges to ensure there are no duplicates. When I run the code step by step, I can see that it sometimes work. I.e. The code will generate a random number, but that number already exists, so it generates a new random number replace it, check again, etc. However, sometimes it will produce a duplicate number and NOT run the code again to replace it. I have no idea why! Any help would be appreciated!
I am using Excel 2010, VBA 7.0
RecipeRows = Input Box based on number of random numbers the user wants
TotalRecipes = A count of used rows in a different sheet
I am using Excel 2010, VBA 7.0
RecipeRows = Input Box based on number of random numbers the user wants
TotalRecipes = A count of used rows in a different sheet
Code:
For Each c In Range("A3:A" & RecipeRows)
TryAgain: c.Value = (Int((TotalRecipes * Rnd) + 1))
If Not Range("A3:A" & c.Row - 1).Find(c.Value, LookIn:=xlValues) Is Nothing Then GoTo TryAgain
Next c