Getting back to the error on this formula. You shouldn't see that error if you've used the code in a new formula created via the 'Field Explorer'.
Have you used this code in the Record Selection or Group Selection Formula?
Please go to the Field Explorer > Right-click Formula Field > New and use the code we discussed above in this formula and place this on the Group Header of Group Footer.
Don't forget that there's one more formula you'll need to create and I've already posted the code for that too.
-Abhilash