|
-
Jun 30th, 2005, 08:31 AM
#1
Thread Starter
Member
Very confused with VBA
First and foremost, I apologize if a lot of my problems are basic, that any beginning programmer should know--- I'm trying to learn, but I'm very new.
I am trying to get a permutation program to work, I am adapting code I found in an online tutorial for VB6 to try to get it to run in VBA.
The foremost problem I encountered was in using dynamic arrays---I couldn't manage to get them to accept values I was trying to read into them.
Example:
VB Code:
Dim Elements() as Long
Dim ii as Long
ii=Val(Display.text)
For i = 1 to ii
Elements(i) = i
'So Element(1) should read 1, etc., I thought. I also tried i = 0 to ii.
Next i
Also, it wouldn't
After I just forced it to work by defining the arrays manually, just to get past it and worry later, I got an "out of stack space" error on the permutate recursion.
I assume this means I had too many iterations-- if so, is there a work-around, or am I just SOL?
Here is the code:
VB Code:
Option Explicit
Public Sub Permutate( _
ByVal ArrayCount As Long, _
ByRef Elements() As Long, _
ByRef Order() As Long, _
ByRef Orders As Collection)
Dim Position As Long
Dim Element As Long
Dim i As Double
Dim ArrayLen As Long
ArrayLen = (UBound(Elements) - LBound(Elements) + 1)
Position = ArrayCount - ArrayLen + 1
If ArrayLen = 1 Then
Order(Position) = Elements(LBound(Elements))
'Here is the comparison spot
Orders.Add Order
Else
For i = LBound(Elements) To UBound(Elements)
Element = Elements(i)
Order(Position) = Element
Permutate ArrayCount, RemoveFromArray(Elements, Element), Order, Orders
Next i
End If
MsgBox Orders
End Sub
Public Function RemoveFromArray(ByRef Elements() As Long, ByVal Element As Long) As Long()
Dim NewArray() As Long
Dim i As Long
Dim newi As Long
' Will create a new array where Element has been left out.
ReDim NewArray(LBound(Elements) To UBound(Elements))
For i = LBound(Elements) To UBound(Elements)
If Elements(i) <> Element Then
newi = newi + 1
NewArray(newi) = Elements(i)
End If
Next
RemoveFromArray = NewArray
End Function
Private Sub Button_Click()
Dim x As Byte
Dim ArrayCount As Long
Dim Elements(9) As Long
Dim Order(9) As Long
Dim Orders As Collection
Dim i As Long
x = 10
'x = Val(Display.Text)
ArrayCount = x
'Later, this will read real values into the array
For i = 0 To 9
Elements(i) = i + 1
Next i
Permutate ArrayCount, Elements(), Order(), Orders
End Sub
Thanks for any help you can offer.
-
Jun 30th, 2005, 08:57 AM
#2
Frenzied Member
Re: Very confused with VBA
If you want to change the size of an array, use Redim, or Redim Preserve if you want to keep the values already in the array. The help file should have plenty of info on it.
Tengo mas preguntas que contestas
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|