You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- random data selection

Advertisement

QUESTION: Bob;

you've helped me before, which i am so grateful for!

Hope you can help me again. I have a spreadsheet with 3 columns

Col A - operator ID, Col B - claim number and Col C - date.

The sheet is sorted in Operator ID order. I need a routine - Macro, preferably, which would randomly select 3 claims for each operator (non-repeating) and if possible, create a new worksheet of this random selection. So i will end up with 3 random records for each operator (consisting of all 3 columns, A,B,C).

your help is ALWAYS much appreciated!

George.

ANSWER: Sub ThreeOfEach()

Set curr = ActiveSheet

Set sel = Selection

Range("D1:E1").Value = 0

Range("D2:D" & Range("A65000").End(xlUp).Row) = [row(1:65000)]

Range("E2:E" & Range("A65000").End(xlUp).Row).FormulaR1C1 = "=rc1&rand()"

[a1].CurrentRegion.Sort key1:=[e2], order1:=xlAscending

Worksheets.Add

Set nw = ActiveSheet

curr.Range("A1:C4").Copy Range("A1")

i = 2

Do

n = curr.Columns(1).Find(curr.Cells(i, 1).Value, searchdirection:=xlPrevious).Row

n = n + 1

If curr.Cells(n, 1).Value = "" Then GoTo es

curr.Cells(n, 1).Resize(3, 3).Copy Cells(Range("A60000").End(xlUp).Row + 1, 1)

i = n

Loop

es:

curr.Activate

[a1].CurrentRegion.Sort key1:=[d2], order1:=xlAscending

Range("D:E").Delete

nw.Activate

Columns.AutoFit

End Sub

[an error occurred while processing this directive]---------- FOLLOW-UP ----------

QUESTION: Bob;

this is the macro you gave me in January and it works great and we are using it every week. Now, is there a way to modify the macro to instead of selecting 3 random claims for each operator; to select a set percentage of random claims for each operator: e.g.: 20% of random claims for each operator. Your help is greatly appreciated!!!

George.

Sub APercentOfEach()

Dim arr()

Dim arrcount As Integer

Set curr = ActiveSheet

Set sel = Selection

pct = Application.InputBox("What percent?", Type:=1)

If pct > 100 Or pct = 0 Then

MsgBox "Enter a value between 1 and 100"

Exit Sub

End If

Range("D1:E1").Value = 0

Range("D2:D" & Range("A65000").End(xlUp).Row) = [row(1:65000)]

Range("E2:E" & Range("A65000").End(xlUp).Row).FormulaR1C1 = "=rc1&rand()"

Range("F2:F" & Range("A65000").End(xlUp).Row).FormulaR1C1 = "=INT(" & pct / 100 & "*COUNTIF(c1,rc1))"

[a1].CurrentRegion.Sort key1:=[e2], order1:=xlAscending

num = [f2].Value

[g2].FormulaArray = "=match(true,offset(A2,1,0,1000,1)<>A2,0)"

nx = [g2].Value

[g2].Copy

Set g = [g2]

ReDim Preserve arr(1)

arr(1) = 2

arrcount = 1

Do

Set g = g.Offset(nx)

arrcount = arrcount + 1

ReDim Preserve arr(arrcount)

arr(arrcount) = g.Row

g.PasteSpecial

If Application.IsNA(g) Then Exit Do

nx = g.Value

g.Copy

Loop

Worksheets.Add

Set nw = ActiveSheet

For i = 1 To arrcount - 1

numtocopy = curr.Cells(arr(i), 6).Value

If i = 1 Then numtocopy = numtocopy + 1

curr.Cells(IIf(i = 1, 1, arr(i)), 1).Resize(numtocopy, 3).Copy Range("A60000").End(xlUp).Offset(1)

Next

Rows(1).Delete

[d1].Value = pct & "% of operators represented."

es:

curr.Activate

[a1].CurrentRegion.Sort key1:=[d2], order1:=xlAscending

Range("D:G").Delete

nw.Activate

Columns.AutoFit

End Sub

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | Thank you very much!!! Works beautifuly and response time was fantastic - within an hour !!!! Can't say enough about your response. thanks again, George. |

This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

Answers by Expert:

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."**Publications**

Excellence, The Expert, Microsoft**Education/Credentials**

BA in math, Hofstra University, 1965**Awards and Honors**

MVP

Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks