Please note I've made a slight amendment to the formula (position of final bracket or brace). I have also updated the attachment,