You can use VBA, and it may work better if you do, but it's not absolutely necessary. In fact, after a little playing around I managed to make the formula solution work with just one helping column, which surprised me; I thought it would take more. See the attached.
For the cost of just one helping column I'd probably stick with that. But you can write your own worksheet function, if you'd prefer. Sometimes they're fun, but they do take longer to run so if you have more than a few hundred of these, you may decide to stick with the formula.
|