Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Trying the line below to populate 2 ranges with random nums between 1-100,
but it returns the same num, albeit random, in every cell. How to tweak/enhance it so that every cell would be populated with a different random num (no duplicates)? Thanks [a1:d4,g1:h5].value = Int((100* Rnd) + 1) |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
You have to generate a new number for each cell. Sub aaa() For Each cell In Range("A1:D4, G1:h5") cell.Value = Int((100 * Rnd) + 1) Next End Sub Regards, Per On 18 Jan., 09:36, "Max" wrote: Trying the line below to populate 2 ranges with random nums between 1-100, but it returns the same num, albeit random, in every cell. How to tweak/enhance it so that every cell would be populated with a different random num (no duplicates)? Thanks [a1:d4,g1:h5].value = Int((100* Rnd) + 1) |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks, Per. That does the mechanics well. But I noticed some of the nums
generated could repeat, which is not desired. How to make it so that there are no duplicates ? |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
This will only allow unique numbers:
Sub aaa() Dim tRng1 As Range Dim tRng2 As Range Set tRng1 = Range("A1:D4") Set tRng2 = Range("G1:H5") For Each cell In Range("A1:D4, G1:h5") Do cell.Value = Int((100 * Rnd) + 1) Loop Until WorksheetFunction.CountIf(tRng1, cell.Value) _ + WorksheetFunction.CountIf(tRng2, cell.Value) = 1 Next End Sub Regards, Per On 18 Jan., 09:58, "Max" wrote: Thanks, Per. That does the mechanics well. But I noticed some of the nums generated could repeat, which is not desired. How to make it so that there are no duplicates ? |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Many thanks, Per. That does it good.
Could you explain how this part of it helps in ensuring uniqueness? Thanks Loop Until WorksheetFunction.CountIf(tRng1, cell.Value) _ + WorksheetFunction.CountIf(tRng2, cell.Value) = 1 Max |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Max
Thanks for your reply, I'm glad to help. It's part of a Do ... Loop The loop will generate a new random value in "cell" until there is only one instance of the number in the target range(s). Hopes this help. Per On 18 Jan., 11:17, "Max" wrote: Many thanks, Per. That does it good. Could you explain how this part of it helps in ensuring uniqueness? Thanks * *Loop Until WorksheetFunction.CountIf(tRng1, cell.Value) _ * * * *+ WorksheetFunction.CountIf(tRng2, cell.Value) = 1 Max |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Here is a slight simplification of the sub
Sub aaa() Dim tRng As Range Set tRng = Union(Range("A1:D4"),Range("G1:H5")) For Each cell In tRng Do cell.Value = Int((100 * Rnd) + 1) Loop Until WorksheetFunction.CountIf(tRng, cell.Value) = 1 Next End Sub The OP asked how the loop helped in making the values unique and the answer is that the exit criteria of the loop is that the number of occurances (CountIf) , in the combined range (tRng), of the just generated random number (cell.Value) is one. Hope this helps / Lars-Åke On Sun, 18 Jan 2009 01:50:00 -0800 (PST), Per Jessen wrote: This will only allow unique numbers: Sub aaa() Dim tRng1 As Range Dim tRng2 As Range Set tRng1 = Range("A1:D4") Set tRng2 = Range("G1:H5") For Each cell In Range("A1:D4, G1:h5") Do cell.Value = Int((100 * Rnd) + 1) Loop Until WorksheetFunction.CountIf(tRng1, cell.Value) _ + WorksheetFunction.CountIf(tRng2, cell.Value) = 1 Next End Sub Regards, Per On 18 Jan., 09:58, "Max" wrote: Thanks, Per. That does the mechanics well. But I noticed some of the nums generated could repeat, which is not desired. How to make it so that there are no duplicates ? |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Lars, thanks for the thoughts
I hit an error 1004 when I tried running your sub: Unable to get countif property of the worksheetFunction class Debug pointed at this line: Loop Until WorksheetFunction.CountIf(tRng, cell.Value) = 1 Max |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Random numbers from several ranges | Excel Worksheet Functions | |||
Help with populating new combobox with list of ranges | Excel Programming | |||
Populating a list box with specific ranges | Excel Programming | |||
Have column of nums, need qty of each? | Excel Worksheet Functions | |||
Is there an example of a DB procedure that adds a col of nums | Excel Programming |