The yardage and the weight matter, but you need the yardage called for, you can substitute a different weight. To find out how many balls you need in the new yarn, you have to find out how many yards of the recommended yarn were called for:
of yards in one ball of yarn called for x # of balls called for. then,
of yards in one ball of new yarn x # of balls, keep multiplying until you find out how many balls you need.
To substitute a different weight, just get the recommended gauge.