Autofilter 10 percent of entire data vba

Deepti

New Member
Joined
May 18, 2018
Messages
4
Hi all,

I am looking for vba whereby i can filter 10 percent of data for audit percent randomly. can anyone suggest the vba code for the same
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
The code below
- column A determines the number of rows
- text "Select" is added in column K (column A = column 1 offset by 10 columns= column K)
- message box added at end listing row number and value of cel in column A

To test
- create a simple sheet containing values only in column A
- amend the 2 constants

Code:
Sub AuditSample()
    Const sName = "[COLOR=#ff0000]MySheetName"[/COLOR]
    Const myOffset = [COLOR=#ff0000]10[/COLOR]
    Dim cel As Range, rng As Range, msg As String
    
    With Sheets(sName)
        Set rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    End With
    
    For Each cel In rng
        If WorksheetFunction.RandBetween(1, 10) = 1 Then
            cel.Offset(, myOffset) = "Select"
            msg = msg & vbCr & cel.Row & vbTab & cel.Value
        End If
    Next
    MsgBox msg, vbOKOnly, "Selected for audit"
End Sub
 
Last edited:
Upvote 0
If you prefer the list created on a different sheet use something like this
- amend constant to name of sheet containing the data

Code:
Sub AuditSheet()
    Const sName = "[COLOR=#ff0000]MySheetName[/COLOR]"
    Dim cel As Range, rng As Range, ws As Worksheet, r As Long
    
    Set ws = Worksheets.Add: ws.Name = "Audit": ws.Range("A1:B1").Value = Array("Row", "Item")
    r = 1
    With Sheets(sName)
        Set rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    End With
    
    For Each cel In rng
        If WorksheetFunction.RandBetween(1, 10) = 1 Then
            r = r + 1
            ws.Cells(r, 1).Resize(, 2) = Array(cel.Row, cel.Value)
        End If
    Next
End Sub
 
Last edited:
Upvote 0
If you prefer the list created on a different sheet use something like this
- amend constant to name of sheet containing the data

Code:
Sub AuditSheet()
    Const sName = "[COLOR=#ff0000]MySheetName[/COLOR]"
    Dim cel As Range, rng As Range, ws As Worksheet, r As Long
    
    Set ws = Worksheets.Add: ws.Name = "Audit": ws.Range("A1:B1").Value = Array("Row", "Item")
    r = 1
    With Sheets(sName)
        Set rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    End With
    
    For Each cel In rng
        If WorksheetFunction.RandBetween(1, 10) = 1 Then
            r = r + 1
            ws.Cells(r, 1).Resize(, 2) = Array(cel.Row, cel.Value)
        End If
    Next
End Sub

Thank you for providing but this is giving the Rand number. I want that 10 percent of entire data get filtered and paste into other sheet can you suggest
 
Upvote 0
To copy the whole row perhaps..

REPLACE
Code:
ws.Cells(r, 1).Resize(, 2) = Array(cel.Row, cel.Value)
WITH
Code:
cel.Resize(, Columns.Count).Copy ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
To copy the whole row perhaps..


REPLACE
Code:
ws.Cells(r, 1).Resize(, 2) = Array(cel.Row, cel.Value)
WITH
Code:
cel.Resize(, Columns.Count).Copy ws.Range("A" & Rows.Count).End(xlUp).Offset(1)
Thank you for your support. But still the issue is not resolved. Every time the output is changing though my criteria is Same. Really can't understand why the output count is changing
 
Upvote 0
The count should be same everytime I run the code 10percent of data. However when I run the code you provided the count of rows changes after every run. Can you suggest
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top