Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

Process a range from bottom to top

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cubbybear3 - 25 Jan 2008 21:56 GMT
I am retrieving a table from a webpage and the data is sorted in
reverse order (by date).  It is a dynamic range but I compute the last
row of the range before processing.  I am currently resorting the data
so I can process it by date (ascending).  My question is how can
modify my current code to read from the bottom of the range to the top
(to eliminate the need for the sort)?  Thanks for any ideas/
suggestions.  -pb

Dim lngLast As Long
Dim rngCell As Range
' find the last row
lngLast = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
If (lngLast < 4) Then   ' first row (if data exists) is always in row
4
   Exit Sub
   End If
' sort the WebData
Range("A4:E" & lngLast).Select
Selection.Sort _
   Key1:=Range("A4"), _
   Order1:=xlAscending, _
   Header:=xlNo, _
   OrderCustom:=1, _
   MatchCase:=False, _
   Orientation:=xlTopToBottom, _
   DataOption1:=xlSortNormal
' Loop through the WebData
For Each rngCell In Worksheets("Sheet1").Range("A4:A" & lngLast)
   ' process table here
   Next
Conan Kelly - 25 Jan 2008 22:28 GMT
cubbybear3,

I would add a long data type variable and change your for loop:

Dim lngIndex as long
'For Each rngCell In Worksheets("Sheet1").Range("A4:A" & lngLast)
'Change your previous loop above to this:
For lngIndex = lngLast to 4 step -1
Next lngIndex

And then inside the loop, wherever you need to process rngCell, change it to
"Cells(lngIndex,1)" (you might have to add "Worksheets("Sheet1")." to the
"Cells()" as well)

HTH,

conan

>I am retrieving a table from a webpage and the data is sorted in
> reverse order (by date).  It is a dynamic range but I compute the last
[quoted text clipped - 26 lines]
>    ' process table here
>    Next
Don Guillett - 25 Jan 2008 22:30 GMT
lngLast = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
for i =lnglast to 4 step-1
 do your thing
next i

Signature

Don Guillett
Microsoft MVP Excel
SalesAid Software
dguillett1@austin.rr.com

>I am retrieving a table from a webpage and the data is sorted in
> reverse order (by date).  It is a dynamic range but I compute the last
[quoted text clipped - 26 lines]
>    ' process table here
>    Next
cubbybear3 - 25 Jan 2008 22:46 GMT
Muchas gracias Conan & Don!

(Conan, thanks for answering what would
have been my next question: how to access
rngCell and any offsets inside the loop.)

-pb
Conan Kelly - 25 Jan 2008 23:06 GMT
Glad to help!!! :-D
> Muchas gracias Conan & Don!
>
[quoted text clipped - 3 lines]
>
> -pb
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.