The saying first appeared in print in the early 1800s. There is a suggestion that it refers to the action of a shepherd counting his flock. In other words, he would flick/shake his stick towards an animal each time he counted one. Thus, 'more than you can shake a stick at' referred to so many that they were virtually uncountable.