ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reverse Values in an Array (https://www.excelbanter.com/excel-programming/395315-reverse-values-array.html)

John

Reverse Values in an Array
 
I have a one-dimensional array containing values that load a listbox. I
would like to reverse the order of the values as they appear in the listbox.
The array is called UniqueValues and I'm loading the listbox via the
following:

For Each Item In UniqueValues
frmTrace.ListBox1.AddItem Item
Next Item

Is there a way to reverse the values without dropping the values into a
worksheet, resorting, and reloading? I've referenced Chip Pearson's site and
he shows the
"ReverseArrayInPlace" procedure but I'm not understanding how it works.

http://www.cpearson.com/excel/VBAArrays.htm

Thanks for your help.

--
John




Helmut Weber[_2_]

Reverse Values in an Array
 
Hi John,

this one fills an array(1 to 50) with random numbers
in the range from 1 to 50 and puts them in listbox1,
as they were created.
it puts them in another listbox2 in reverse order.

Private Sub CommandButton1_Click()
Dim x(1 To 50) As Long
Dim y
Randomize
For y = 1 To 50
x(y) = Int((50 * Rnd) + 1)
Me.ListBox1.AddItem x(y)
Next
For y = 50 To 1 Step -1
Me.ListBox2.AddItem x(y)
Next
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

OssieMac

Reverse Values in an Array
 
Hi John,

Try this.

For i = UBound(UniqueValues) To 0 Step -1
frmTrace.ListBox1.AddItem UniqueValues(i)
Next i

or this:

For i = UBound(UniqueValues) To LBound(UniqueValues) Step -1
frmTrace.ListBox1.AddItem UniqueValues(i)
Next i

The second one doesn't matter whether UniqueValues(0) or UniqueValues(1) is
the lowest.


Regards,

OssieMac
"John" wrote:

I have a one-dimensional array containing values that load a listbox. I
would like to reverse the order of the values as they appear in the listbox.
The array is called UniqueValues and I'm loading the listbox via the
following:

For Each Item In UniqueValues
frmTrace.ListBox1.AddItem Item
Next Item

Is there a way to reverse the values without dropping the values into a
worksheet, resorting, and reloading? I've referenced Chip Pearson's site and
he shows the
"ReverseArrayInPlace" procedure but I'm not understanding how it works.

http://www.cpearson.com/excel/VBAArrays.htm

Thanks for your help.

--
John




Rick Rothstein \(MVP - VB\)

Reverse Values in an Array
 
I have a one-dimensional array containing values that load a listbox. I
would like to reverse the order of the values as they appear in the
listbox.
The array is called UniqueValues and I'm loading the listbox via the
following:

For Each Item In UniqueValues
frmTrace.ListBox1.AddItem Item
Next Item

Is there a way to reverse the values without dropping the values into a
worksheet, resorting, and reloading?


Use the same code you have, but add the optional argument to the AddItem
method...

For Each Item In UniqueValues
frmTrace.ListBox1.AddItem Item, 0
Next Item

Rick


John Mansfield

Reverse Values in an Array
 
Thanks Helmut, OssieMac, and Rick for your time and help - all examples
worked perfectly.

--
John






"Rick Rothstein (MVP - VB)" wrote:

I have a one-dimensional array containing values that load a listbox. I
would like to reverse the order of the values as they appear in the
listbox.
The array is called UniqueValues and I'm loading the listbox via the
following:

For Each Item In UniqueValues
frmTrace.ListBox1.AddItem Item
Next Item

Is there a way to reverse the values without dropping the values into a
worksheet, resorting, and reloading?


Use the same code you have, but add the optional argument to the AddItem
method...

For Each Item In UniqueValues
frmTrace.ListBox1.AddItem Item, 0
Next Item

Rick




All times are GMT +1. The time now is 11:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com