ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to reverse data in an Array (https://www.excelbanter.com/excel-programming/349417-how-reverse-data-array.html)

Kevin O'Neill[_2_]

How to reverse data in an Array
 
Dim c() As Integer
Dim ends(20) As Integer
For d = Sheet11.Cells(18, 14).Value - 1 To 0 Step -1
ends(d) = Range("A50000").End(xlUp).Row
c() = ends()
Next d 'end stud loop
ReDim Preserve c(Sheet11.Cells(18, 14).Value - 1)

The data in the table is backwards. I assume due to the step -1? Is
there a VBA command to reverse the data in c() ?

Ex. from 1,2,3...to 3,2,1...

Suggestions?


Bob Phillips[_6_]

How to reverse data in an Array
 
Something like this

For i = LBound(ary) To UBound(ary) / 2
tmp = ary(UBound(ary) - i)
ary(UBound(ary) - i) = ary(i)
ary(i) = tmp
Next i


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Kevin O'Neill" wrote in message
oups.com...
Dim c() As Integer
Dim ends(20) As Integer
For d = Sheet11.Cells(18, 14).Value - 1 To 0 Step -1
ends(d) = Range("A50000").End(xlUp).Row
c() = ends()
Next d 'end stud loop
ReDim Preserve c(Sheet11.Cells(18, 14).Value - 1)

The data in the table is backwards. I assume due to the step -1? Is
there a VBA command to reverse the data in c() ?

Ex. from 1,2,3...to 3,2,1...

Suggestions?




Dave Peterson

How to reverse data in an Array
 
Why not just change your for statement:

For d = Sheet11.Cells(18, 14).Value - 1 To 0 Step -1
becomes
For d = 0 to Sheet11.Cells(18, 14).Value - 1

But all that did for me was put in the row number of the last used cell in
column A in all the elements of the array.

I bet that's not what you really wanted.

(Ps. The original code did the same thing.)

========
If you wanted to get the last x number of values from column A, you could use
something like:

Option Explicit
Sub testme04()

Dim ends As Variant
Dim HowMany As Long
Dim LastCell As Range
dim iCtr as long

With sheet11
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
HowMany = .Cells(18, 14).Value
ends = application.transpose(LastCell.Offset(1 - HowMany, 0) _
.Resize(HowMany, 1).Value)
End With

'to prove it works
for ictr = lbound(ends) to ubound(ends)
msgbox ictr & "--" & ends(ictr)
next ictr

End Sub

Kevin O'Neill wrote:

Dim c() As Integer
Dim ends(20) As Integer
For d = Sheet11.Cells(18, 14).Value - 1 To 0 Step -1
ends(d) = Range("A50000").End(xlUp).Row
c() = ends()
Next d 'end stud loop
ReDim Preserve c(Sheet11.Cells(18, 14).Value - 1)

The data in the table is backwards. I assume due to the step -1? Is
there a VBA command to reverse the data in c() ?

Ex. from 1,2,3...to 3,2,1...

Suggestions?


--

Dave Peterson

Kevin O'Neill[_2_]

How to reverse data in an Array
 
Thanks, ended up using this.

ends(ab) = Range("A50000").End(xlUp).Row
ab = ab + 1
c() = ends()
ReDim Preserve c(Sheet11.Cells(18, 14).Value - 1)



All times are GMT +1. The time now is 01:23 PM.

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