Hi
Are you sharing with us (part of) your homework assignment?
Well, it's too telegraphic for me.
I'd need
- a brief explanation of what this is about
- the logic (what you posted a tad more elaborated)
- a sample of data, input and output expected
- what type of solution you need, vba or formula
- the excel version you have
I believe you'd get more answers with a more complete post.
It may be enough, however, for some other members and I hope, of course, that you get a quick solution.