LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 415
Default create range of cells with random numbers

Assuming I was correct in my last post:

Private Sub CommandButton1_Click()
Dim RetVal As Variant
Dim FullRange As Range
Dim EntriesCount As Long
Dim BinaryArray() As Long

On Error Resume Next

Set RetVal = Application.InputBox("Enter or select the range to fill.", , ,
, , , , 8)
If RetVal Is Nothing Then Exit Sub
Set FullRange = RetVal

RetVal = Application.InputBox("How many entries in total.", , , , , , , 2)
'2 ?
If RetVal = False Then Exit Sub
If RetVal FullRange.Cells.Count Then
MsgBox "Not enough cells in the selected range for " & RetVal & "
entries.", vbExclamation + vbOKOnly
Exit Sub
End If

EntriesCount = RetVal

GenerateRandomArray EntriesCount, BinaryArray()

DistributeBinaryValues FullRange, BinaryArray()

End Sub

Private Function GenerateRandomArray(ElementCount As Long, ByRef
ArrayToFill() As Long)
Dim i As Long

ReDim ArrayToFill(ElementCount - 1)
Randomize
For i = 0 To ElementCount - 1
If Rnd() 0.5 Then
ArrayToFill(i) = 1
Else
ArrayToFill(i) = 0
End If
Next
End Function

Private Function DistributeBinaryValues(ByRef argFullRange As Range,
BinaryValues() As Long)
Dim PlacedCount As Long
Dim RandRow As Long
Dim RandCol As Long
Dim MaxRow As Long
Dim MaxCol As Long

argFullRange.ClearContents

MaxRow = argFullRange.Rows.Count
MaxCol = argFullRange.Columns.Count

Do Until PlacedCount = UBound(BinaryValues) + 1
RandRow = CLng(Rnd * (MaxRow - 1) + 1)
'CLng(Rnd * (high - low)) + low
RandCol = CLng(Rnd * (MaxCol - 1) + 1)

With Cells(RandRow, RandCol)
Debug.Print .Address
If Cells(RandRow, RandCol).Value = "" Then
.Value = BinaryValues(PlacedCount)
PlacedCount = PlacedCount + 1
End If
End With
Loop

End Function

NickHK

"NickHK" ...
So you need 3 states; blank, 0 or 1 ?
With the count of 0s and 1s even to say 100 ?

NickHk


egroups.com...
I would like to, from VBA in Excel, be able to select a range of cells.
And then enter a number for instance 100 and randomly distribute zeros
and ones over the previously selected range of cells to a total of 100.
Some help would be appreciated.





 
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
How to? Create random letters and numbers in a cell Chris Excel Worksheet Functions 3 June 5th 08 08:04 AM
How to generate random numbers from a range of cells? fruitchunk Excel Worksheet Functions 3 December 7th 07 04:49 AM
How can I create a list of random numbers with no duplicates? Kwasniewski Excel Discussion (Misc queries) 2 May 15th 06 02:44 AM
create random NON-REPEATING numbers (e.g., 20 #'s between 1-100) Leon Excel Discussion (Misc queries) 1 June 3rd 05 12:20 AM
How do I create formula to count numbers in a range of cells? EmilyJ Excel Worksheet Functions 1 December 8th 04 05:24 AM


All times are GMT +1. The time now is 12:27 AM.

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"