Randomly pick/select cells in a range
Hi Howard,
Am Thu, 12 Nov 2015 04:20:32 -0800 (PST) schrieb L. Howard:
With code, how would I randomly select/pick say 8 cells in the range B5 to O5, and only if blank?
the more cells that are not empty the less empty cells you can pick.Your
range only has 14 cells.
But try:
Sub RandomCells()
Dim rngBig As Range
Dim varRnd() As Variant, varNew() As Variant
Dim i As Long
Dim myDic As Object
Do
n = Application.Min(Int(Rnd * 15) + 2, 15)
If Len(Cells(5, n)) = 0 Then
ReDim Preserve varRnd(i)
varRnd(i) = n
i = i + 1
End If
Loop Until i = 14
Set myDic = CreateObject("Scripting.Dictionary")
For i = LBound(varRnd) To UBound(varRnd)
myDic(varRnd(i)) = varRnd(i)
Next
varNew = myDic.items
For i = 0 To myDic.Count - 1
If rngBig Is Nothing Then
Set rngBig = Cells(5, varNew(i))
Else
Set rngBig = Union(rngBig, Cells(5, varNew(i)))
End If
Next
rngBig.Select
End Sub
Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
|