Marco said:
Hi,
I need your help to find a solution for the following issue:
I have a file with a list of cards in column B. I need a macro t select
randomly 5% of the cards and copy it in column D.
Is possible to do this?
Thanks for your attention
or this:
some explanation in the code as comments:
VBA Code:
--------------------
Sub blah()
'Using a single column of data whose top DATA cell is defined by TLCell and whose bottomost cell is defined as the cell above the first blank cell below TLCell,
'This macro uses the column immediately to the right briefly, to put formulae in and leaves results in the column to the right of that.
'It will overwrite anything in the two columns immediately to the right of the TLCell column.
'It returns a percent (determined by the percentage variable) of the data (at random) at the top of a column 2 to the right of the TLCell column.
Dim TLCell As Range
Set TLCell = Range("B2") 'adjust to top cell of card DATA, not the header.
percentage = 5 'adjust to the percent you want to see. (eg. 5 = 5%)
Application.ScreenUpdating = False
Set Col1 = Range(TLCell, TLCell.End(xlDown))
With Col1
Col1Addr = Col1.Address(ReferenceStyle:=xlR1C1)
Col2Addr = Col1.Offset(, 1).Address(ReferenceStyle:=xlR1C1)
lastrw = .Count + .Row - 1
.Offset(, 1).FormulaR1C1 = "=RAND()"
.Offset(, 2).FormulaR1C1 = "=IF(ROW()-" & .Row - 1 & "<=(ROUND(" & .Count & "*" & percentage / 100 & _
",0)),INDEX(" & Col1Addr & ",MATCH(LARGE(" & Col2Addr & ",row()-" & .Row - 1 & ")," & Col2Addr & ",0)),"""")"
.Offset(, 2).Value = .Offset(, 2).Value
.Offset(, 1).Clear
End With
Application.ScreenUpdating = True
End Sub