Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Please improve this simple implementation

Please help me improve the implementation below. The VBA comment
highlights the improvement I am looking for. Thanks.

PS: Does "for each cell in rangeVar" always select from the first to
last cell in the range? I coulda sworn that I observed that it did
not, despite what the help page seems to say. But after I made some
changes to the spreadsheet, it consistently worked as I expected
(first to last). I wonder if the "for each" order depends on the
Excel evaluation order, which not only depends on the dependency
graph, but also seems to depend on the order in which cells have been
modified (I think).


' return relative index of last matching cell in range

Function matchlast(v As Variant, r As Range) As Long
Dim i As Long

' we should select from last to first cells in range;
' that would avoid searching the entire range every
' time. but I do not remember how to do that.

matchlast = 0
i = 0
For Each cell In r
i = i + 1
If cell = v Then matchlast = i
Next cell
End Function
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Please improve this simple implementation

Try something like the following. It returns the 1-based offset into
the range if the value is found or an #N/A error if not found.


Function MatchLast(V As Variant, R As Range) As Variant
Dim N As Long
With R.Cells
For N = .Count To 1 Step -1
If .Cells(N).Value = V Then
MatchLast = N
Exit Function
End If
Next N
End With
MatchLast = CVErr(xlErrNA) ' not found
End Function

Cordially,
Chip Pearson
Microsoft MVP
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email is on the web site)
USA Central Daylight Time (-5:00 GMT)



On Sun, 12 Oct 2008 09:58:24 -0700 (PDT),
wrote:

Please help me improve the implementation below. The VBA comment
highlights the improvement I am looking for. Thanks.

PS: Does "for each cell in rangeVar" always select from the first to
last cell in the range? I coulda sworn that I observed that it did
not, despite what the help page seems to say. But after I made some
changes to the spreadsheet, it consistently worked as I expected
(first to last). I wonder if the "for each" order depends on the
Excel evaluation order, which not only depends on the dependency
graph, but also seems to depend on the order in which cells have been
modified (I think).


' return relative index of last matching cell in range

Function matchlast(v As Variant, r As Range) As Long
Dim i As Long

' we should select from last to first cells in range;
' that would avoid searching the entire range every
' time. but I do not remember how to do that.

matchlast = 0
i = 0
For Each cell In r
i = i + 1
If cell = v Then matchlast = i
Next cell
End Function

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 85
Default Please improve this simple implementation

On Oct 12, 10:38*am, Chip Pearson wrote:
* * * * For N = .Count To 1 Step -1
* * * * * * If .Cells(N).Value = V Then


Thanks, Chip. That's exactly what I needed.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions implementation history gecs Excel Discussion (Misc queries) 0 August 18th 09 07:13 PM
RTD C# implementation on vs2005 Sheldon Excel Programming 0 September 8th 07 12:28 AM
Implementation Guidance [email protected] Excel Programming 1 July 3rd 07 02:06 AM
Creation & Implementation of VB6 DLL in VBA to raise events Trip[_3_] Excel Programming 3 November 22nd 06 02:03 AM
UserForm implementation Fernando Ronci Excel Programming 1 August 24th 04 02:46 PM


All times are GMT +1. The time now is 12:06 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"