I can help a little bit. My main suggestion was about a state indicator. Basically, if an IF statement is true, the THEN clause is performed. And true is indicated by a numeric 1. A false is a zero.
My suggestion had four bullets:
1. default setting is on
Do steps 2-4 in a loop.
2. turn off (set to zero) when you find the first record
3. If (on) then process record
If (usethis) Then
4. turn on (set to one) when you find the last record.