Modified post:
I am working on tick bars. Within my afl code, I have retrieved the start bar of a set of bars as 57544 and end bar as 64634. In this set of bars, I find the lowest price is 11855.4 and highest price is 11936. Within these bars, I have marked the lower bound price as 11858.3 and upper bound price as 11872.8.

What I am trying to find is the following:

First bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8

Last bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8

I tried from my end but without success. Let me put some partial work.

BarIdxArr = BarIndex();
priceBoundArr = IIf(Close >= lowerBound AND Close <= upperBound, 1, 0);
firstBar = ValueWhen(priceBoundArr, BarIdxArr);

Original post:
I have a set of bars with the startBar and endBar values. Within this set of bars, I am trying to find the first bar and last bar of all the bars that lie within two price values.

I tried searching but didn't find it on the forum. I did go through the following link Bars since higher or equal value, but it didn't help.

Let me put the question properly.
I am working on tick bars. Within my afl code, I have retrieved the start bar of a set of bars as 57544 and end bar as 64634. In this set of bars, I find the lowest price is 11855.4 and highest price is 11936. Within these bars, I have marked the lower bound price as 11858.3 and upper bound price as 11872.8.

What I am trying to find is the following:

First bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8

Last bar between bars 57544 and 64634 that lies within the prices 11858.3 and 11872.8

I tried from my end but without success. Let me put some partial work.

BarIdxArr = BarIndex();
priceBoundArr = IIf(Close >= lowerBound AND Close <= upperBound, 1, 0);
firstBar = ValueWhen(priceBoundArr, BarIdxArr);

Thanks @Cougar, I was able to get the desired result with your help. The only thing, I had to add was LastValue for the output from ValueWhen.

Find below my code to get the first and last bars. startBar and endBar are populated within afl depending on the bars selected.

bi = BarIndex();
rangeArr = bi >= startBar AND bi <= endBar AND C >= lowerBound AND C <= upperBound;
rangeCum = Cum(rangeArr);
firstBar = LastValue(ValueWhen(rangeCum == 1 AND rangeArr, bi));
lastBar = LastValue(ValueWhen(rangeCum == LastValue(rangeCum) AND rangeArr, bi));