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

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Random selection of rows.

Heera,

Assuming that your data starts in A1, try the macro below.

HTH,
Bernie
MS Excel MVP


Sub SelectRandomSample()
Dim mySh1 As Worksheet
Dim mySh2 As Worksheet
Dim myR As Long

Set mySh1 = ActiveSheet
Application.DisplayAlerts = False
On Error Resume Next
Worksheets("Random Selection").Delete
Application.DisplayAlerts = True
Set mySh2 = Worksheets.Add
mySh2.Name = "Random Selection"

mySh1.Cells.Copy mySh2.Cells

mySh2.Range("A1").EntireColumn.Insert
mySh2.Range("A1").Value = "Randomize"
myR = mySh2.Cells(Rows.Count, 2).End(xlUp).Row
mySh2.Range("A2:A" & myR).FormulaR1C1 = "=RAND()"
Range("A1").CurrentRegion.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:= _
xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

If MsgBox("Keep 10%? ""No"" to select number.", vbYesNo) = vbYes Then
Rows(myR / 10).Resize(myR).EntireRow.Delete
Else
Rows(InputBox("How many rows to keep?") + 2).Resize(myR).EntireRow.Delete
End If
mySh2.Range("A1").EntireColumn.Delete
End Sub


"chavan2000 via OfficeKB.com" <u47671@uwe wrote in message news:90dff53897926@uwe...
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



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
Random Selection carl Excel Worksheet Functions 7 June 13th 08 11:12 PM
Random Selection Mandeep Dhami Excel Programming 1 September 27th 06 01:10 PM
Random Selection John Excel Programming 2 May 20th 06 06:31 PM
Random Selection Cookie New Users to Excel 2 May 3rd 06 12:05 AM
Random Selection Newdlj[_3_] Excel Programming 3 December 23rd 03 12:55 AM


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

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"