Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Excel Macro?

Thread Tools
 
Search this Thread
 
Old 14 October 2003, 08:18 PM
  #1  
ianmiller999
Scooby Regular
Thread Starter
 
ianmiller999's Avatar
 
Join Date: Feb 2003
Posts: 1,285
Likes: 0
Received 0 Likes on 0 Posts
Post

Ok I have to take a random data sample say 200 people from a a excel file with about 1000 people. Is there any simple code to this or has anyone already created a macro to do this all will it be quicker for me to manually randomly select data.
Old 15 October 2003, 10:49 AM
  #2  
markr1963
Scooby Regular
 
markr1963's Avatar
 
Join Date: Jun 2002
Location: Perth, Western Australia
Posts: 1,866
Likes: 0
Received 0 Likes on 0 Posts
Post

Attach this bit of code to a macro

For i = 1 To 200
j = Int((1000 * Rnd) + 1)
Cells(i, 5).Value = Cells(j, 1)
Next i

It takes 200 random values from column A and puts them in column E. Change the code to suit.

Its cheap and cheerful but should work.

Edited to add: I just spotted a flaw - duplicates! I'll try and sort that out later.

Mark

[Edited by markr1963 - 10/15/2003 11:04:01 AM]
Old 15 October 2003, 12:01 PM
  #3  
markr1963
Scooby Regular
 
markr1963's Avatar
 
Join Date: Jun 2002
Location: Perth, Western Australia
Posts: 1,866
Likes: 0
Received 0 Likes on 0 Posts
Post

Had another go.

Dim a(1000) As Variant

For i = 1 To 200
redo:
Randomize
j = Int((1000 * Rnd) + 1)

For k = 1 To i ' checks for match in array
If j = a(k) Then ' if there's a match generate another number
GoTo redo
End If
Next k
Cells(i, 5).Value = Cells(j, 1)
a(i) = j ' adds value generated to array
Next i

Mark
Old 16 October 2003, 12:46 PM
  #4  
ianmiller999
Scooby Regular
Thread Starter
 
ianmiller999's Avatar
 
Join Date: Feb 2003
Posts: 1,285
Likes: 0
Received 0 Likes on 0 Posts
Post

Thankyou
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Mattybr5@MB Developments
Full Cars Breaking For Spares
28
28 December 2015 11:07 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
12
18 November 2015 07:03 AM
leg200
Subaru Parts
5
07 October 2015 07:31 AM
has-scooby
Subaru Parts
4
06 October 2015 03:47 PM
bluebullet29
General Technical
9
05 October 2015 02:17 PM



Quick Reply: Excel Macro?



All times are GMT +1. The time now is 07:30 PM.