?
Solved

EXCEL: Random Select and Copy 10% of the Row

Posted on 2016-10-26
7
Medium Priority
?
362 Views
Last Modified: 2016-11-01
I have been copying random 10% of entire row of data into the new sheet. For example, i have 1000 row of the data, i have to select and copy 10% row of data (which is 100) randomly into the new sheet. Been doing this on weekly basis.

Is there way to do it automatically by selecting and copying 10% of row into the new sheet using VBA Excel?. It would be great if you can create VBA macro code to detect the entire number of row automatically and select 10% of the row randomly.

 I have attached mock spreadsheet attachment and you can use the example to random select 10% of the row into new sheet

Please let me know if you have issue.
SelectRandom.xlsx
0
Comment
Question by:Sherry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Ejgil Hedegaard
ID: 41861205
Check attached file.
Press button to run.

This is the code

Option Explicit

Sub Extract10Percent()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rwMax As Long, rw As Long
    
    Application.ScreenUpdating = False
    
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets.Add
    ws1.Range("A2").CurrentRegion.Copy ws2.Range("A1")
    rwMax = ws2.Range("A1").CurrentRegion.Rows.Count
    ws2.Range("D1") = "Random"
    For rw = 2 To rwMax
        ws2.Range("D" & rw) = WorksheetFunction.RandBetween(1, 10)
    Next rw
    ws2.Range("A1").CurrentRegion.Sort Key1:="Random", Order1:=xlAscending, Header:=xlYes
    rw = WorksheetFunction.RoundUp((rwMax - 1) / 10, 0) + 2
    ws2.Range("D1:D" & rwMax).ClearContents
    ws2.Range("A" & rw & ":C" & rwMax).ClearContents
    ws2.Range("A1").CurrentRegion.Sort Key1:="No", Order1:=xlAscending, Header:=xlYes
End Sub

Open in new window

SelectRandom.xlsm
0
 

Author Comment

by:Sherry
ID: 41861309
Ejgil Hedegaard

Many thanks for the prompt solution, it has met my expectation entirely and very interesting coding. I have one more query.

If i have 30 column of data, is there way that your code that identify the last column instead of putting column "D" in your coding.

Just incase if i have come across with different number of column

Many thanks
0
 

Author Comment

by:Sherry
ID: 41862589
Apologies another query, I have changed the number to 5 in the coding and it doesn't generate 5% number of alert.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 23

Accepted Solution

by:
Ejgil Hedegaard earned 2000 total points
ID: 41862675
The values (1 to 10) in the RandBetween formula does not set the percentage, so changing 10 to 5 does not change the number of output rows.
The random values are not used to select the rows, so it can be anything (more or less), but is used to set the sorting values.
I think it will be better with more random values, so I have changed the interval to 1 to 1000.
The number of result rows is in the RoundUp formula.
Divide by 10 is equal to 10 %, changed to accept any percentage.
Code changed to accept any number of columns.
Set the percentage in cell K1, and run.
It is a named range (the name is used in the program), so you can move (not copy) the cell to another location.

Sub ExtractPercent()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rwMax As Long, rw As Long, colMax As Integer
    
    Application.ScreenUpdating = False
    
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets.Add
    ws1.Range("A2").CurrentRegion.Copy ws2.Range("A1")
    rwMax = ws2.Range("A1").CurrentRegion.Rows.Count
    colMax = ws2.Range("A1").CurrentRegion.Columns.Count + 1
    ws2.Cells(1, colMax) = "Random"
    For rw = 2 To rwMax
        ws2.Cells(rw, colMax) = WorksheetFunction.RandBetween(1, 1000)
    Next rw
    ws2.Range("A1").CurrentRegion.Sort Key1:="Random", Order1:=xlAscending, Header:=xlYes
    rw = WorksheetFunction.RoundUp((rwMax - 1) * [ExtractPercentage], 0) + 2
    ws2.Range(Cells(1, colMax), Cells(rwMax, colMax)).ClearContents
    ws2.Range(Cells(rw, 1), Cells(rwMax, colMax)).ClearContents
    ws2.Range("A1").CurrentRegion.Sort Key1:="No", Order1:=xlAscending, Header:=xlYes
    ws2.Columns.AutoFit
End Sub

Open in new window

SelectRandom.xlsm
0
 

Author Comment

by:Sherry
ID: 41862839
Thanks Ejigil

That was smart coding but received coding error when i run them. see attached

Good idea on the custom percentage
Screen-Shot-2016-10-27-at-19.29.25.png
Screen-Shot-2016-10-27-at-19.32.57.png
0
 
LVL 23

Expert Comment

by:Ejgil Hedegaard
ID: 41863016
Guess you used the code in another workbook, where the named range ExtractPercentage is missing.
Name the cell with the percentage value, and it will work.
The name must be ExtractPercentage, or if you use something else, change the name in the brackets [ ] to whatever you name it.
0
 

Author Closing Comment

by:Sherry
ID: 41869146
Excellent Answer and Interesting VBA coding. Expectation met
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question