select random 30% of given row

manojxls

New Member
Joined
Dec 21, 2017
Messages
24
Office Version
  1. 365
  2. 2007
  3. 2003 or older
Platform
  1. Windows
Hi,
please help me select column b ,random 30% of given "po" in column.
data is large, so please help me a formula.

abc
1barcode1po
2barcode2po
3barcode5po
4barcode3non-po
5barcode3po
6barcode7po
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

It would also help clarify if you gave a slightly larger sample (say 20-30 rows) and sample results, preferably with XL2BB, so that we can see just what sort of results layout you want.
 
Upvote 0
It is quite difficult to generate unique randoms via formula. Please try the code below:
VBA Code:
Sub test()
   Dim barcodes As Variant, rnds As Variant, lRow As Long, i As Long, rnd As Long, pickNumber As Long
   lRow = Cells(Rows.Count, "B").End(xlUp).Row
   barcodes = Range("B1:C" & lRow)
   pickNumber = lRow * 0.3
   ReDim rnds(0)
   With Application
   For i = 1 To pickNumber
     Do
       rnd = .WorksheetFunction.RandBetween(1, pickNumber)
     Loop While Not IsError(.Match(rnd, rnds, 0))
     barcodes(i, 2) = barcodes(rnd, 1)
     rnds(UBound(rnds)) = rnd
     ReDim Preserve rnds(UBound(rnds) + 1)
   Next
   End With
   Range("B1").Resize(UBound(barcodes, 1), UBound(barcodes, 2)).Value = barcodes
 End Sub
 
Upvote 0
I am sorry, I both got your question wrong and my code had a flaw. Please find the updated code:
VBA Code:
Sub test()
 Dim barcodes As Variant, rnds As Variant, lRow As Long, i As Long, rnd As Long, pickNumber As Long
 lRow = Cells(Rows.Count, "B").End(xlUp).Row
 barcodes = Range("A1:C" & lRow)
 pickNumber = lRow * 0.3
 ReDim rnds(0)
 With Application
 For i = 1 To pickNumber
  Do
   rnd = .WorksheetFunction.RandBetween(1, lRow)
  Loop While Not IsError(.Match(rnd, rnds, 0)) Or Not barcodes(rnd, 2) = "po"
  barcodes(i, 3) = barcodes(rnd, 1)
  rnds(UBound(rnds)) = rnd
  ReDim Preserve rnds(UBound(rnds) + 1)
 Next
 End With
 Range("A1").Resize(UBound(barcodes, 1), UBound(barcodes, 2)).Value = barcodes
 End Sub
 
Upvote 0
hi,
let say forgot "c" column
suppose from 500 row say column "a" i want to select or filter only 30% from given 500 row.
like if "a" column row is 10 it will select only 3 as 30%.
 
Upvote 0
This must do the job
VBA Code:
Sub test()
  Dim barcodes As Variant, rnds As Variant, lRow As Long, i As Long, rnd As Long, pickNumber As Long
  lRow = Cells(Rows.Count, "B").End(xlUp).Row
  barcodes = Range("A1:B" & lRow)
  Redim Preserve barcodes (1 To lRow, 1 To 3)
  pickNumber = lRow * 0.3
  ReDim rnds(0)
  With Application
  For i = 1 To pickNumber
    Do
      rnd = .WorksheetFunction.RandBetween(1, lRow)
    Loop While Not IsError(.Match(rnd, rnds, 0)) Or Not barcodes(rnd, 2) = "po"
  barcodes(i, 3) = barcodes(rnd, 1)
  rnds(UBound(rnds)) = rnd
  ReDim Preserve rnds(UBound(rnds) + 1)
  Next
  End With
  For i = 1 To lRow
    If i <= pickNumber Then  
      barcodes(i, 1) = barcodes(i, 3)
      barcodes(i, 2) = "po"
    Else
      barcodes(i, 1) = ""
      barcodes(i, 2) = ""
    End If
  Next
  Redim Preserve barcodes (1 To lRow, 1 To 2)
  Range("A1").Resize(lRow, 2).Value = barcodes
 End Sub
 
Upvote 0
Would a formula approach like be any good?

manojxls.xlsm
ABCD
1barcode14pobarcode6
2barcode1pobarcode1
3barcode5pobarcode1
4barcode4non-pobarcode4
5barcode11pobarcode4
6barcode15non-pobarcode6
7barcode1pobarcode5
8barcode6po
9barcode17po
10barcode11po
11barcode11po
12barcode7po
13barcode6po
14barcode6po
15barcode3po
16barcode17non-po
17barcode4po
18barcode7po
19barcode5po
20barcode19non-po
21barcode13po
22barcode6po
23barcode1po
24barcode4non-po
25barcode8po
26barcode17non-po
27barcode4po
28barcode4po
29barcode5po
Sample
Cell Formulas
RangeFormula
D1:D7D1=LET(f,FILTER(A1:A29,B1:B29="po"),TAKE(SORTBY(f,RANDARRAY(ROWS(f))),ROUND(ROWS(f)*0.3,0)))
Dynamic array formulas.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,124
Messages
6,123,190
Members
449,090
Latest member
bes000

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