Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 12
Default How to Set Range to 50 Random nonsequencial rows?

I have a spreadsheet with 4000 rows. I choose 50 of those rows using the excel
random number generator as in:

LastRow = Sheets(OS).Cells(Rows.Count, "a").End(xlUp).Row
LastCol = Sheets(OS).Range("A2").End(xlToRight).Column
N= LastRow
For lj = 1 To 50
ll = Int(N * Rnd) + 1
lRndRows(lj) = ll
Next lj

How would I set the range as in:

Dim rng As Range
Dim myCell As Range
Dim curwk As Worksheet
OS = ActiveSheet.Name
Set curwk = Sheets(OS)
With Curwk
Set rng=?????
end with

to those 50 rows?

Thanks for any help

Dennis
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default How to Set Range to 50 Random nonsequencial rows?

Your first routine pick a set of rows at random and stores them in a array.
You need to make a range out of this set:


Sub routine()
Dim lRndRows(51)
OS = "OS"
LastRow = Sheets(OS).Cells(Rows.Count, "a").End(xlUp).Row
LastCol = Sheets(OS).Range("A2").End(xlToRight).Column
N = LastRow
For lj = 1 To 50
ll = Int(N * Rnd) + 1
lRndRows(lj) = ll
Next lj
Dim rng As Range
Set rng = Range("A" & lRndRows(1)).EntireRow
For i = 2 To 50
Set rng = Union(rng, Range("A" & lRndRows(i)).EntireRow)
Next
MsgBox (rng.Address)
rng.Select
End Sub

--
Gary''s Student
gsnu200710


" wrote:

I have a spreadsheet with 4000 rows. I choose 50 of those rows using the excel
random number generator as in:

LastRow = Sheets(OS).Cells(Rows.Count, "a").End(xlUp).Row
LastCol = Sheets(OS).Range("A2").End(xlToRight).Column
N= LastRow
For lj = 1 To 50
ll = Int(N * Rnd) + 1
lRndRows(lj) = ll
Next lj

How would I set the range as in:

Dim rng As Range
Dim myCell As Range
Dim curwk As Worksheet
OS = ActiveSheet.Name
Set curwk = Sheets(OS)
With Curwk
Set rng=?????
end with

to those 50 rows?

Thanks for any help

Dennis

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 12
Default How to Set Range to 50 Random nonsequencial rows?

Actually I think I found an easier way.

Dim rng As Range
Dim myCell As Range
Dim curwk As Worksheet
dim OS as string

OS = ActiveSheet.Name
Set curwk = Sheets(OS)
With Curwk
Set rng = Nothing
For lj = 1 To 50
ll = Int(N * Rnd) + 1
Set rng = rng + .Rows(ll)
Next lj
end with

Gary''s Student wrote:

Your first routine pick a set of rows at random and stores them in a array.
You need to make a range out of this set:


Sub routine()
Dim lRndRows(51)
OS = "OS"
LastRow = Sheets(OS).Cells(Rows.Count, "a").End(xlUp).Row
LastCol = Sheets(OS).Range("A2").End(xlToRight).Column
N = LastRow
For lj = 1 To 50
ll = Int(N * Rnd) + 1
lRndRows(lj) = ll
Next lj
Dim rng As Range
Set rng = Range("A" & lRndRows(1)).EntireRow
For i = 2 To 50
Set rng = Union(rng, Range("A" & lRndRows(i)).EntireRow)
Next
MsgBox (rng.Address)
rng.Select
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 12
Default How to Set Range to 50 Random nonsequencial rows?

Actually I think I found an easier way.

Dim rng As Range
Dim myCell As Range
Dim curwk As Worksheet
dim OS as string

OS = ActiveSheet.Name
Set curwk = Sheets(OS)
With Curwk
Set rng = Nothing
For lj = 1 To 50
ll = Int(N * Rnd) + 1
Set rng = rng + .Rows(ll)
Next lj
end with

Gary''s Student wrote:

Your first routine pick a set of rows at random and stores them in a array.
You need to make a range out of this set:


Sub routine()
Dim lRndRows(51)
OS = "OS"
LastRow = Sheets(OS).Cells(Rows.Count, "a").End(xlUp).Row
LastCol = Sheets(OS).Range("A2").End(xlToRight).Column
N = LastRow
For lj = 1 To 50
ll = Int(N * Rnd) + 1
lRndRows(lj) = ll
Next lj
Dim rng As Range
Set rng = Range("A" & lRndRows(1)).EntireRow
For i = 2 To 50
Set rng = Union(rng, Range("A" & lRndRows(i)).EntireRow)
Next
MsgBox (rng.Address)
rng.Select
End Sub

  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 12
Default How to Set Range to 50 Random nonsequencial rows?

Actually I think I found an easier way.

Dim rng As Range
Dim myCell As Range
Dim curwk As Worksheet
dim OS as string

OS = ActiveSheet.Name
Set curwk = Sheets(OS)
With Curwk
Set rng = Nothing
For lj = 1 To 50
ll = Int(N * Rnd) + 1
Set rng = rng + .Rows(ll)
Next lj
end with

Gary''s Student wrote:

Your first routine pick a set of rows at random and stores them in a array.
You need to make a range out of this set:


Sub routine()
Dim lRndRows(51)
OS = "OS"
LastRow = Sheets(OS).Cells(Rows.Count, "a").End(xlUp).Row
LastCol = Sheets(OS).Range("A2").End(xlToRight).Column
N = LastRow
For lj = 1 To 50
ll = Int(N * Rnd) + 1
lRndRows(lj) = ll
Next lj
Dim rng As Range
Set rng = Range("A" & lRndRows(1)).EntireRow
For i = 2 To 50
Set rng = Union(rng, Range("A" & lRndRows(i)).EntireRow)
Next
MsgBox (rng.Address)
rng.Select
End Sub



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 92
Default How to Set Range to 50 Random nonsequencial rows?

Hello,

You know that sometimes you will end up with less than 50 selected
rows?

If you need 50 different rows:
http://www.sulprobil.com/html/uniqrandint.html

Regards,
Bernd

  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 12
Default How to Set Range to 50 Random nonsequencial rows?

Thanks Bernd!!!

I didn't think of that. If you ask for 50 random numbers to represent 50 unique
random rows, sometimes some of the 50 random rows will be duplicates. Thus one
has to ask for 50 unique random rows. Your code was very helpful.

Dennis

"Bernd" wrote:

Hello,

You know that sometimes you will end up with less than 50 selected
rows?

If you need 50 different rows:
http://www.sulprobil.com/html/uniqrandint.html

Regards,
Bernd

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
Unique RANDOM NUMBERS within specified range Matt D Francis Excel Worksheet Functions 5 July 15th 08 08:04 PM
Random within range [email protected] Excel Worksheet Functions 0 May 24th 06 02:44 PM
Random within range [email protected] Excel Worksheet Functions 0 May 24th 06 02:44 PM
Formula for random shuffle between a range (example) b4,b78 m spangler Excel Worksheet Functions 4 May 9th 05 07:05 PM
random selection from a range of cells tjb Excel Worksheet Functions 1 February 15th 05 06:34 PM


All times are GMT +1. The time now is 01:23 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"