C
curiousgeorge408
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
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