Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Simplifying code using array

Is there a way to say that MyArray(5) cannot equal any element of the
array, without listing each element, as below?
Do
MyArray(5) = Int(10 * Rnd)
Loop Until MyArray(5) < MyArray(4) And _
MyArray(5) < MyArray(3) And _
MyArray(5) < MyArray(2) And _
MyArray(5) < MyArray(1)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Simplifying code using array

Sub Tester13()
Dim myArray(1 To 10)
For i = 1 To 10
Do
temp = Int(10 * Rnd + 1)
bMatch = False
For j = 1 To i - 1
If myArray(j) = temp Then
bMatch = True
Exit For
End If
Next
Loop While bMatch
myArray(i) = temp
sStr = sStr & "myarray(" & i & ")= " & myArray(i) & vbNewline
Next
msgbox sStr
End Sub


--
Regards,
Tom Ogilvy




John Pierce wrote in message
om...
Is there a way to say that MyArray(5) cannot equal any element of the
array, without listing each element, as below?
Do
MyArray(5) = Int(10 * Rnd)
Loop Until MyArray(5) < MyArray(4) And _
MyArray(5) < MyArray(3) And _
MyArray(5) < MyArray(2) And _
MyArray(5) < MyArray(1)



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Simplifying code using array

Hi John,
I assume you would like to make an unique number list in array...
The random number value sequence not overlapping is generated by
shuffling the arrangement which put in order the value which can
usually be taken.


Code:
--------------------

Sub TestShuffleArrayAsRandom()
Const n As Long = 5
Dim MyArray() As Long
Dim i As Long
Dim r As Long
Dim tmp

ReDim MyArray(0 To n)

'Make a sample MyArrayay
For i = 0 To n
MyArray(i) = i
Next

'Shuffle Array
For i = 1 To n
r = Int(Rnd() * n) + 1
tmp = MyArray(i): MyArray(i) = MyArray(r): MyArray(r) = tmp
Next
Stop 'Pls take a look at MyArray

End Sub

--------------------



---
Message posted from http://www.ExcelForum.com/

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Simplifying code using array

Sorry, I found a little mistake. Please modify my code as follows.

Code:
--------------------

'Shuffle Array
For i = 0 To n

--------------------



---
Message posted from http://www.ExcelForum.com/

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
Need help simplifying VBA code jlclyde Excel Discussion (Misc queries) 4 September 4th 08 04:21 PM
Code needs simplifying Sandy Excel Worksheet Functions 5 April 26th 07 01:31 PM
Simplifying VBA code Michael M Excel Worksheet Functions 8 January 24th 07 02:17 PM
Simplifying array formula which evaluates as error. Richard Buttrey Excel Worksheet Functions 5 September 30th 05 02:35 AM
Code to determine if a cell contains an array function? [email protected] Excel Discussion (Misc queries) 1 August 29th 05 10:35 PM


All times are GMT +1. The time now is 05:41 AM.

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

About Us

"It's about Microsoft Excel"