View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Random selection of rows.


Sub Randrows()

'Set sheet where data is located
Set DataSht = Sheets("Sheet1")
'set column where random numbe is placed
RandCol = "X"
'initialized random generator
Randomize


With DataSht
LastRow = .Range("A" & Rows.Count).End(xlUp).Row
NumRows = InputBox("Enter Number of Rows (1 to " & LastRow & ") to Get")

For RowCount = 1 To LastRow
.Range("X" & RowCount) = Rnd()
Next
'sort data by random number generator
.Rows("1:" & LastRow).Sort _
key1:=.Range("X1"), _
order1:=xlAscending, _
header:=xlNo

Set newsht = Sheets.Add(after:=Sheets(Sheets.Count))
If NumRows LastRow Then
NumRows = LastRow
End If
.Rows("1:" & NumRows).Copy _
Destination:=newsht.Rows(1)
End With

"chavan2000 via OfficeKB.com" wrote:

Hi,

I have a database of 10000 rows in excel and i want a macro which will select
10%(10% of 10000 rows) of rows randomly and paste it in a different sheet.

And if a user mentions a specific number, say 1150 rows then the macro should
select those many rows randomly and paste it in a different sheet.

I dont know how to use the rand() function in VBA. Can sum one help me with
the code.
I am not a programmer

Regards
Heera

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200901/1