Results 1 to 2 of 2

Thread: Very confused with VBA

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    41

    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:
    1. Dim Elements() as Long
    2. Dim ii as Long
    3. ii=Val(Display.text)
    4. For i = 1 to ii
    5. Elements(i) = i
    6. 'So Element(1) should read 1, etc., I thought. I also tried i = 0 to ii.
    7. 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:
    1. Option Explicit
    2.  
    3. Public Sub Permutate( _
    4.    ByVal ArrayCount As Long, _
    5.    ByRef Elements() As Long, _
    6.    ByRef Order() As Long, _
    7.    ByRef Orders As Collection)
    8.     Dim Position   As Long
    9. Dim Element    As Long
    10. Dim i          As Double
    11. Dim ArrayLen   As Long
    12.  
    13.    
    14.    ArrayLen = (UBound(Elements) - LBound(Elements) + 1)
    15.      
    16.    Position = ArrayCount - ArrayLen + 1
    17.    
    18.    If ArrayLen = 1 Then
    19.       Order(Position) = Elements(LBound(Elements))
    20.       'Here is the comparison spot
    21.       Orders.Add Order
    22.    Else
    23.       For i = LBound(Elements) To UBound(Elements)
    24.          Element = Elements(i)
    25.          Order(Position) = Element
    26.          Permutate ArrayCount, RemoveFromArray(Elements, Element), Order, Orders
    27.       Next i
    28.    
    29.    End If
    30.    MsgBox Orders
    31. End Sub
    32.  
    33. Public Function RemoveFromArray(ByRef Elements() As Long, ByVal Element As Long) As Long()
    34.  
    35. Dim NewArray() As Long
    36. Dim i          As Long
    37. Dim newi       As Long
    38.  
    39.    ' Will create a new array where Element has been left out.
    40.  
    41.    ReDim NewArray(LBound(Elements) To UBound(Elements))
    42.    For i = LBound(Elements) To UBound(Elements)
    43.       If Elements(i) <> Element Then
    44.          newi = newi + 1
    45.          NewArray(newi) = Elements(i)
    46.       End If
    47.    Next
    48.    
    49.    RemoveFromArray = NewArray
    50.  
    51. End Function
    52.  
    53.  
    54. Private Sub Button_Click()
    55. Dim x As Byte
    56. Dim ArrayCount As Long
    57. Dim Elements(9) As Long
    58. Dim Order(9) As Long
    59. Dim Orders As Collection
    60. Dim i As Long
    61. x = 10
    62. 'x = Val(Display.Text)
    63. ArrayCount = x
    64. 'Later, this will read real values into the array
    65. For i = 0 To 9
    66. Elements(i) = i + 1
    67. Next i
    68. Permutate ArrayCount, Elements(), Order(), Orders
    69. End Sub
    Thanks for any help you can offer.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    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
  •  



Click Here to Expand Forum to Full Width