Reverse Array (yarra?)

Reverse Array (yarra?)

Anonymous
Not applicable
464 Views
5 Replies
Message 1 of 6

Reverse Array (yarra?)

Anonymous
Not applicable
I have a quick question - In a VBA routine - I have an array of an undetermined amount, could be two items or twenty items. what I think I want to do is to reverse the order they are in. i.e. (1, 2, 3, 4) becomes (4, 3, 2, 1). Is there a SIMPLE way to do this?? The size of the array is based on the user pressing the "ADD" command button, which redimensions the array. Any help would be greatly appreciated.
Thanks
Dave. K
0 Likes
465 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
Here's a way I have done it, there may be something
more efficient out there?

 

Sub Test()
  Dim sArray(5) As
String
  Dim vTemp As Variant
  Dim i As Integer
 

  sArray(0) = "A"
  sArray(1) = "B"
  sArray(2) =
"C"
  sArray(3) = "D"
  sArray(4) = "E"
  sArray(5) =
"F"
   
  Do
    vTemp =
sArray(i)
    sArray(i) = sArray(UBound(sArray) -
i)
    sArray(UBound(sArray) - i) =
vTemp
    i = i + 1
  Loop Until i = (UBound(sArray) +
(UBound(sArray) Mod 2)) / 2

 

  For i = LBound(sArray) To
UBound(sArray)
    Debug.Print sArray(i)
 
Next

 

End Sub

 

 

Regards,

  Jacob Dinardi


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I
have a quick question - In a VBA routine - I have an array of an undetermined
amount, could be two items or twenty items. what I think I want to do is to
reverse the order they are in. i.e. (1, 2, 3, 4) becomes (4, 3, 2, 1). Is
there a SIMPLE way to do this?? The size of the array is based on the user
pressing the "ADD" command button, which redimensions the array. Any help
would be greatly appreciated.
Thanks
Dave. K
0 Likes
Message 3 of 6

Anonymous
Not applicable
Thanks, I'll give that a try. Looks like it will do the trick, But I sure wish there was something a little simpler. In LISP all I need do is "(reverse LIST)" Oh well I guess if it was easy everybody'd be writing programs!
SEEYA!

Dave. K
0 Likes
Message 4 of 6

Anonymous
Not applicable
It would be nice to have a built in function, but
this is pretty simple, 6 lines of code to actually do the work... you can make
your own function and reuse it.

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Thanks,
I'll give that a try. Looks like it will do the trick, But I sure wish there
was something a little simpler. In LISP all I need do is "(reverse LIST)" Oh
well I guess if it was easy everybody'd be writing programs!
SEEYA!

Dave. K

0 Likes
Message 5 of 6

Anonymous
Not applicable
Instead of reversing the array, just step thru it
backwards.

 

for i = ubound(myArray) to lbound(myArray)
-1

    'do your stuff



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I
have a quick question - In a VBA routine - I have an array of an undetermined
amount, could be two items or twenty items. what I think I want to do is to
reverse the order they are in. i.e. (1, 2, 3, 4) becomes (4, 3, 2, 1). Is
there a SIMPLE way to do this?? The size of the array is based on the user
pressing the "ADD" command button, which redimensions the array. Any help
would be greatly appreciated.
Thanks
Dave. K
0 Likes
Message 6 of 6

Anonymous
Not applicable
Oops, got a little quick on the draw, that's:

for i = ubound(myArray) to lbound(myArray) Step -1


"Bobby C. Jones" wrote in message
news:9B4D4013B2CB55C9153D43A78C6C6E0D@in.WebX.maYIadrTaRb...
Instead of reversing the array, just step thru it backwards.

for i = ubound(myArray) to lbound(myArray) -1
'do your stuff
next i
--
Bobby C. Jones
www.AcadX.com
"KingCAD" wrote in message
news:f102de4.-1@WebX.maYIadrTaRb...
I have a quick question - In a VBA routine - I have an array of an
undetermined amount, could be two items or twenty items. what I think I want
to do is to reverse the order they are in. i.e. (1, 2, 3, 4) becomes (4, 3,
2, 1). Is there a SIMPLE way to do this?? The size of the array is based on
the user pressing the "ADD" command button, which redimensions the array.
Any help would be greatly appreciated.
Thanks
Dave. K
0 Likes