Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default Randomly pick/select cells in a range

With code, how would I randomly select/pick say 8 cells in the range B5 to O5, and only if blank?

Thanks,
Howard
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default Randomly pick/select cells in a range

Hi Claus,

Thanks for the code.

Real close, is there a way to refer to a cell on the sheet for the exact number of cells to randomly select.

The value would be in Q5, which I declared as 8.

On a second run of the code the value will be in R5 and will be 3.

Third run is for the value in S5 which is 3.

The values may vary from Q, R, & S but will never add to more than 14.

With each run of the code, I will be trying to write some values to those selections. And each set of selected cells will get a value from a different range of values.

And there are six rows to do these three runs on.

May need some help on that, but trying to do it myself for now.

Howard

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Randomly pick/select cells in a range

Hi Howard,

Am Thu, 12 Nov 2015 06:22:26 -0800 (PST) schrieb L. Howard:

The value would be in Q5, which I declared as 8.

On a second run of the code the value will be in R5 and will be 3.

Third run is for the value in S5 which is 3.

The values may vary from Q, R, & S but will never add to more than 14.

With each run of the code, I will be trying to write some values to those selections. And each set of selected cells will get a value from a different range of values.

And there are six rows to do these three runs on.


are the cells at start all empty? Where is the range for these runs?
What do you mean with 6 rows for the 3 runs?
Where are the values the should be entered into the selected cells?
Can you send me a workbook with some examples?


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what can I do to randomly select cells that contain text? gmadden2 Excel Worksheet Functions 2 January 6th 09 05:58 PM
does ctrl f select cells randomly? tc Excel Discussion (Misc queries) 3 August 23rd 07 11:26 PM
How do I use For loop to pick different Range of cells to Select & Merge? [email protected] Excel Programming 3 February 3rd 07 02:02 AM
Randomly select numbers from a range Fred Smith Excel Discussion (Misc queries) 15 October 3rd 06 02:01 AM
Randomly Select Cells Debbie[_6_] Excel Programming 3 August 8th 03 08:11 PM


All times are GMT +1. The time now is 08:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"