I didn't think of using the IF statement.
My thinking is that minerr() should does the work of finding the right combination (5 variables) with minimum error automatically.
Therefore the question to me is to figure out the way to call variables (5 variables as a set) into the minerr() in each loop and also the rule to choose the next variable set.
Maybe that's where you mean to use the IF statement (the rule to choose the next variable set).
I hope there is a simple way to do it..