Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Selecting random rows


Hi,

Is it possible to select, say 70 random rows from 500 rows? Sorry it's
a very short question, but I don't even know where to start!

Many thanks,

Anar


--
anar_baku
------------------------------------------------------------------------
anar_baku's Profile: http://www.excelforum.com/member.php...o&userid=18259
View this thread: http://www.excelforum.com/showthread...hreadid=509941

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Selecting random rows

Hi Anar,

Here is one way

Dim rng As Range
Do
If rng Is Nothing Then
Set rng = Rows(Fix(Rnd() * 500 + 1))
Else
Set rng = Union(rng, Rows(Fix(Rnd() * 500 + 1)))
End If
Loop Until rng.Areas.Count = 70

rng.Select


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"anar_baku" wrote
in message ...

Hi,

Is it possible to select, say 70 random rows from 500 rows? Sorry it's
a very short question, but I don't even know where to start!

Many thanks,

Anar


--
anar_baku
------------------------------------------------------------------------
anar_baku's Profile:

http://www.excelforum.com/member.php...o&userid=18259
View this thread: http://www.excelforum.com/showthread...hreadid=509941



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Selecting random rows

This is a bit better as it is relative to your target area

Dim rng As Range
Dim iRow As Long
With Rows("10:510")
Do
iRow = Fix(Rnd() * 500 + 1)
If rng Is Nothing Then
Set rng = .Rows(iRow)
Else
Set rng = Union(rng, .Rows(iRow))
End If
Loop Until rng.Areas.Count = 70
End With

rng.Select


--

HTH

Bob Phillips

(remove nothere from the email address if mailing direct)

"anar_baku" wrote
in message ...

Hi,

Is it possible to select, say 70 random rows from 500 rows? Sorry it's
a very short question, but I don't even know where to start!

Many thanks,

Anar


--
anar_baku
------------------------------------------------------------------------
anar_baku's Profile:

http://www.excelforum.com/member.php...o&userid=18259
View this thread: http://www.excelforum.com/showthread...hreadid=509941



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Selecting random rows


Bob,

no guarantee that it wont select two subsequent rows,
where a union would merge the areas.

?union([1:1],[2:2]).areas.Count
1

Alternative:

Sub RandomRows()
Dim d As Object, r As Range, vKeys, x&

'get a set of 70 unique numbers
Set d = CreateObject("Scripting.Dictionary")
While d.Count < 70
'Define the min,max of your numbers
x = RndBetween(10, 510)
If Not d.Exists(x) Then d.Add x,Empty
Wend

'Create a multiarea range
vKeys = d.keys
Set r = Rows(vKeys(0))
For x = 1 To UBound(vKeys)
Set r = Union(r, Rows(vKeys(x)))
Next
'Select it
r.Select

End Sub

Function RndBetween(low&, high&) As Long
RndBetween = CLng(Rnd * (high - low)) + low
End Function








--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Bob Phillips wrote :

This is a bit better as it is relative to your target area

Dim rng As Range
Dim iRow As Long
With Rows("10:510")
Do
iRow = Fix(Rnd() * 500 + 1)
If rng Is Nothing Then
Set rng = .Rows(iRow)
Else
Set rng = Union(rng, .Rows(iRow))
End If
Loop Until rng.Areas.Count = 70
End With

rng.Select

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Selecting random rows

Good point. I think I thought about that at the start, but obviously lost
the thought when doing it <G

Thanks

"keepITcool" wrote in message
.com...

Bob,

no guarantee that it wont select two subsequent rows,
where a union would merge the areas.

?union([1:1],[2:2]).areas.Count
1

Alternative:

Sub RandomRows()
Dim d As Object, r As Range, vKeys, x&

'get a set of 70 unique numbers
Set d = CreateObject("Scripting.Dictionary")
While d.Count < 70
'Define the min,max of your numbers
x = RndBetween(10, 510)
If Not d.Exists(x) Then d.Add x,Empty
Wend

'Create a multiarea range
vKeys = d.keys
Set r = Rows(vKeys(0))
For x = 1 To UBound(vKeys)
Set r = Union(r, Rows(vKeys(x)))
Next
'Select it
r.Select

End Sub

Function RndBetween(low&, high&) As Long
RndBetween = CLng(Rnd * (high - low)) + low
End Function








--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Bob Phillips wrote :

This is a bit better as it is relative to your target area

Dim rng As Range
Dim iRow As Long
With Rows("10:510")
Do
iRow = Fix(Rnd() * 500 + 1)
If rng Is Nothing Then
Set rng = .Rows(iRow)
Else
Set rng = Union(rng, .Rows(iRow))
End If
Loop Until rng.Areas.Count = 70
End With

rng.Select





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
the formula for random selecting Inquiring mind Excel Discussion (Misc queries) 2 March 19th 09 02:33 PM
Selecting Random Data myssieh Excel Worksheet Functions 5 February 13th 08 05:09 PM
selecting random cells Willie Excel Programming 0 January 17th 06 07:13 AM
Selecting Random Data Willie Excel Worksheet Functions 2 August 18th 05 06:26 PM
selecting random records terry freedman Excel Programming 2 April 2nd 04 11:07 AM


All times are GMT +1. The time now is 08:51 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"