Sub routine using rnd and look for values in array

dailan

New Member
Joined
Oct 7, 2016
Messages
20
Dear folks,

hope that all is well.

I am having some troubles with a vba code.
1. Generating random variables within an interval
2. Creating a loop where if the random number generated is within an earlier interval it would generate a new random number

So as for now I only get "1" on rows 10-19. And then I would like to create a loop that checks if the value generated is already in an array above the row of which the generated number is. If it is in this array then the loop would generate another random number.

Please see below code for what I have been attempting;

VBA Code:
Sub RNG_Click()
r
Dim ws1, ws2 As Worksheet
Dim i, Nr_rows, stokastiskt1, RNG As Integer

Sheets("Stickprov").Activate

Set ws1 = Worksheets("Stickprov")
Set ws2 = Worksheets("Beviljade ansökningar")
Nr_rows = ws2.Range("A2").End(xlDown).Row
RNG = Int((Nr_rows * rnd) + 1)
stokastiskt1 = ws1.Range("A3:A9").value


For i = 10 To 19
    
    Cells(i, 1).value = RNG
    
    
       ' If Cells(i, 1) = stokastiskt1 Then
          '  Cells(i, 1).value = RNG
      '  End If
                        
                  
Next i

MsgBox "Sample is provided"


End Sub


As always, many thanks in advance!

Kind regards

Dailan
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
You could explain with an example what data you have and what would be the result you want.

stokastiskt1 = ws1.Range("A3:A9").value
In that range you have 7 data.
And in your cycle you have 10 (For i = 10 To 19)
I think they should have the same number.
 
Upvote 0
Dear DanteAmor,

thanks for your reply.

So in worksheet 2 I have a matrix of raw data.
"
Nr_rows = ws2.Range("A2").End(xlDown).Row
"

This gives me the total number of rows in that array, so no problems there.


And then I try to do the stochastic grab of the rows by using the
"
RNG = Int((Nr_rows * rnd) + 1)
"

As the next step I try to populate the first column in worksheet 1 with randomly picked rows from the total number of rows. A condition is that the picked number should not be the same as a randomly picked number in the array above, and if so, it should continue the loop until it randomly grabs another number.

Hopefully this answers your question; stokastiskt1 is the array above cells(i, 1) that shouldn't contain the randomly picked number. And the cells I would like to populate is rows 10-19. As it is now I get the same value in rows 10-19, and it doesn't take into account whatever value is in the stokastiskt1 array.

For i = 10 To 19

Cells(i, 1).value = RNG


' If Cells(i, 1) = stokastiskt1 Then
' Cells(i, 1).value = RNG
' End If


Next i


Hopefully all above helps to clarify. Please tell me if there something else I have to explain in order for to give a helpful comment.

Thanking in advance!

KR

Daniel
 
Upvote 0
You could explain with an example what data you have and what would be the result you want.

It would be more practical for me to understand with a simple example.

You have 2 sheets.
On sheet2 you have data, do you want to take 10 random data from sheet2 and put it on sheet1?

If not, because I am not understanding.

You could forget your code for a moment and explain with an example what data you have and what you want in the output.

Something like this:

I have this on the sheet "Beviljade ansökningar"
varios 16jul2020 inche caracter.xlsm
A
1
2Data2
3Data3
4Data4
5Data5
6Data6
7Data7
8Data8
9Data9
10Data10
11Data11
12Data12
13Data13
14Data14
15Data15
16Data16
17Data17
18Data18
19Data19
20Data20
21
Beviljade ansökningar

And I want the next Output.
varios 16jul2020 inche caracter.xlsm
A
10
11
12
13
14
15
16
17
18
19
Stickprov


So you put your examples of what you have and the output you want, and explain a couple of those data in the output.

To put examples use the XL2BB tool, look in my signature or here:
 
Upvote 0
Dear DanteAmor,

hope that the weekend was rewarding and that all is well in other regards.

Please see below;

On sheet 2 I have the raw data that I would like to extract to sheet 1. Looks like below. And then I have some additional columns to the right, which are not necessary to display in order to answer the question.

Kundnummer_fullDB
30047035575371205778
20010839943311206165
20055524840391205691
20053628923301206997
30065396597341206497
20048619004391206368
20045664830381206393
30027665504311205971
20084946082361205969
30029058903341206179
20019728358351206305
20022937854361206325
30034901962361205187
30042024256321206009
20032266387341206021
20002853712311205821
20066197603381206144
20081937460301206308
20026655911311207024
30097878281341205851
30070805271301206388
30011460435391206250
30074265034311206364


And what I am trying to achieve is the thing on sheet 1 (stickprov), as below. Rows 3-9 are the rows where I am trying just without creating a subroutine. Rows 10-20 is when using the subroutine. So what I would like to create is a loop where;
1. taking a random row (number) from the first column in sheet 2
2. checking if the randomly picked number already is already picked in the rows above it. If so the loop should continue until the randomly picked number is unique. To take as an example - the number picked in row 11 would breach this criteria thus making it necessary to continue selecting a random number until the number picked is unique.

Cell Formulas
RangeFormula
B3:B22B3=INDEX('Beviljade ansökningar'!$A$2:$R$8528, A3, 12)
C3:C22C3=IF(LEFT(INDEX('Beviljade ansökningar'!$A$2:$R$8979, A3, 1), 3)="100", "Kontantfinans", IF(LEFT(INDEX('Beviljade ansökningar'!$A$2:$R$8979, A3, 1), 3)="200", "Brixo", IF(LEFT(INDEX('Beviljade ansökningar'!$A$2:$R$8979, A3, 1), 3)="300", "Flexkontot", IF(LEFT(INDEX('Beviljade ansökningar'!$A$2:$R$8979, A3, 1), 3)="400", "Merax",))))
D3:D22D3=INDEX('Beviljade ansökningar'!$A$2:$R$8528, A3, 2)
A3A3=RAND()*COUNT('Beviljade ansökningar'!A:A)
A4:A9,A20:A22A4=IF(RAND()*COUNT('Beviljade ansökningar'!A:A)=$A$3:A3, RAND()*COUNT('Beviljade ansökningar'!A:A), RAND()*COUNT('Beviljade ansökningar'!A:A))
Press CTRL+SHIFT+ENTER to enter array formulas.



Hopefully above clarifies a bit more exactly what I am trying to do.
 
Upvote 0
Sorry, but I still don't understand, especially since in your two sheets I don't see an example of a relationship between the two sheets.
Let's do an example with less data.

Assuming, in the "Beviljade ansökningar" sheet you have 10 data, from cell A2 to cell A11, only 10 rows with data, for example:
Dante Amor
ABC
1Kundnummer_fullDB
230047035575371205778
320010839943311206165
420055524840391205691
520053628923301206997
630065396597341206497
720048619004391206368
820045664830381206393
930027665504311205971
1020084946082361205969
1130029058903341206179
12
Beviljade ansökningar

So you have 10 numbers: 1,2,3,4,5,6,7,8,9,10.
You want those 10 numbers to be presented randomly on the "Stickprov" sheet, for example:
Dante Amor
AB
1
2Rank
31
43
55
66
79
84
98
107
1110
122
13
Stickprov

If the above is correct, try this:

VBA Code:
Sub test_arr_1()
  Dim i As Long, x As Long, y As Long, lr As Long
  Dim arr As Variant
  Dim ws1 As Worksheet, ws2 As Worksheet
  
  Set ws1 = Sheets("Stickprov")
  Set ws2 = Worksheets("Beviljade ansökningar")
  lr = ws2.Range("A" & Rows.Count).End(3).Row
  
  arr = Evaluate("=row(1:" & lr - 1 & ")")
  Randomize
  For i = 1 To UBound(arr)
    x = Int(UBound(arr) * Rnd + 1)
    y = arr(x, 1)
    arr(x, 1) = arr(i, 1)
    arr(i, 1) = y
  Next
  ws1.Range("A3").Resize(UBound(arr)).Value = arr
End Sub
 
Upvote 0
Dear DanteAmor,

hope all is well also today.

Your patience is absolutely amazing - many would have given up trying to help me. The solution presented by you works, given that I receive the rows in a random order and I don't risk of getting duplicates.

Many thanks!

Kind regards

Daniel
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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