Hi All,
I am creating a sheet in which I have to select 5 random items out of 20 items. However, if 5 items are selected in round 1, round 2 should have 5 values from the remaining 15 items, round 3 from the remaining 10, and at last the remaining 4.
I am using following code but there is an error after round 1 as values get repeated. Here is my code:
Thanks for your help!
Abhishek
"
Sub Macro2()
'
' Macro2 Macro
'
'
Range("B6").Select
ActiveCell.FormulaR1C1 = "=RAND()"
Range("B6").Select
Selection.AutoFill Destination:=Range("B6:B25"), Type:=xlFillDefault
Range("B6:B25").Select
Range("C6").Select
ActiveCell.FormulaR1C1 = "=INDEX(R6C1:R25C1,RANK(RC[-1],R6C2:R25C2))"
Range("C6").Select
Selection.AutoFill Destination:=Range("C6:C10"), Type:=xlFillDefault
Range("C6:C10").Select
Selection.Copy
Range("D6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("E6").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-4],R6C[-1]:R25C[-1],1,FALSE)),RC[-4],"""")"
Range("E6").Select
Selection.AutoFill Destination:=Range("E6:E25"), Type:=xlFillDefault
Range("E6:E25").Select
Selection.Copy
Range("F6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
ActiveWindow.SmallScroll ToRight:=3
Range("G25").Select
Application.CutCopyMode = False
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=6, Criteria1:="<>"
Range("F7:F25").Select
Selection.Copy
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=6
Range("G6").Select
Selection = Selection.Value
Application.CutCopyMode = False
Range("H6").Select
ActiveCell.FormulaR1C1 = "=RAND()"
Range("H6").Select
Selection.AutoFill Destination:=Range("H6:H20"), Type:=xlFillDefault
Range("H6:H20").Select
ActiveWindow.SmallScroll ToRight:=2
Range("I6:I10").Select
Selection.Copy
Range("J6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("K6").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-4],R6C[-1]:R20C[-1],1,FALSE)),RC[-4],"""")"
Range("K6").Select
Selection.AutoFill Destination:=Range("K6:K20"), Type:=xlFillDefault
Range("K6:K20").Select
Selection.Copy
Range("L6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
ActiveWindow.SmallScroll ToRight:=3
Range("N25").Select
Application.CutCopyMode = False
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=12, Criteria1:="<>"
Range("L6:L20").Select
Selection.Copy
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=12
Range("M6").Select
Selection = Selection.Value
Range("N6").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RAND()"
Range("N6").Select
Selection.AutoFill Destination:=Range("N6:N10"), Type:=xlFillDefault
Range("N6:N10").Select
Range("O6").Select
ActiveCell.FormulaR1C1 = "=INDEX(R6C13:R15C13,RANK(RC[-1],R6C14:R15C14))"
Range("O6").Select
Selection.AutoFill Destination:=Range("O6:O10"), Type:=xlFillDefault
Range("O6:O10").Select
Selection.Copy
Range("P6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll ToRight:=3
Range("Q6").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-4],R6C[-1]:R15C[-1],1,FALSE)),RC[-4],"""")"
Range("Q6").Select
Selection.AutoFill Destination:=Range("Q6:Q15"), Type:=xlFillDefault
Range("Q6:Q15").Select
Selection.Copy
Range("R6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Range("R21").Select
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=18, Criteria1:="<>"
Range("R11:R15").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=18
Range("S6").Select
Selection = Selection.Value
End Sub
"
I am creating a sheet in which I have to select 5 random items out of 20 items. However, if 5 items are selected in round 1, round 2 should have 5 values from the remaining 15 items, round 3 from the remaining 10, and at last the remaining 4.
I am using following code but there is an error after round 1 as values get repeated. Here is my code:
Thanks for your help!
Abhishek
"
Sub Macro2()
'
' Macro2 Macro
'
'
Range("B6").Select
ActiveCell.FormulaR1C1 = "=RAND()"
Range("B6").Select
Selection.AutoFill Destination:=Range("B6:B25"), Type:=xlFillDefault
Range("B6:B25").Select
Range("C6").Select
ActiveCell.FormulaR1C1 = "=INDEX(R6C1:R25C1,RANK(RC[-1],R6C2:R25C2))"
Range("C6").Select
Selection.AutoFill Destination:=Range("C6:C10"), Type:=xlFillDefault
Range("C6:C10").Select
Selection.Copy
Range("D6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("E6").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-4],R6C[-1]:R25C[-1],1,FALSE)),RC[-4],"""")"
Range("E6").Select
Selection.AutoFill Destination:=Range("E6:E25"), Type:=xlFillDefault
Range("E6:E25").Select
Selection.Copy
Range("F6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
ActiveWindow.SmallScroll ToRight:=3
Range("G25").Select
Application.CutCopyMode = False
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=6, Criteria1:="<>"
Range("F7:F25").Select
Selection.Copy
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=6
Range("G6").Select
Selection = Selection.Value
Application.CutCopyMode = False
Range("H6").Select
ActiveCell.FormulaR1C1 = "=RAND()"
Range("H6").Select
Selection.AutoFill Destination:=Range("H6:H20"), Type:=xlFillDefault
Range("H6:H20").Select
ActiveWindow.SmallScroll ToRight:=2
Range("I6:I10").Select
Selection.Copy
Range("J6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("K6").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-4],R6C[-1]:R20C[-1],1,FALSE)),RC[-4],"""")"
Range("K6").Select
Selection.AutoFill Destination:=Range("K6:K20"), Type:=xlFillDefault
Range("K6:K20").Select
Selection.Copy
Range("L6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
ActiveWindow.SmallScroll ToRight:=3
Range("N25").Select
Application.CutCopyMode = False
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=12, Criteria1:="<>"
Range("L6:L20").Select
Selection.Copy
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=12
Range("M6").Select
Selection = Selection.Value
Range("N6").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RAND()"
Range("N6").Select
Selection.AutoFill Destination:=Range("N6:N10"), Type:=xlFillDefault
Range("N6:N10").Select
Range("O6").Select
ActiveCell.FormulaR1C1 = "=INDEX(R6C13:R15C13,RANK(RC[-1],R6C14:R15C14))"
Range("O6").Select
Selection.AutoFill Destination:=Range("O6:O10"), Type:=xlFillDefault
Range("O6:O10").Select
Selection.Copy
Range("P6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.SmallScroll ToRight:=3
Range("Q6").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-4],R6C[-1]:R15C[-1],1,FALSE)),RC[-4],"""")"
Range("Q6").Select
Selection.AutoFill Destination:=Range("Q6:Q15"), Type:=xlFillDefault
Range("Q6:Q15").Select
Selection.Copy
Range("R6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=True, Transpose:=False
Range("R21").Select
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=18, Criteria1:="<>"
Range("R11:R15").Select
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Range("$A$5:$S$25").AutoFilter Field:=18
Range("S6").Select
Selection = Selection.Value
End Sub
"