I created two worksheets, Data1 and Data2, and filled column 2 of each with row numbers. Running your script when Data2 is active gave this result which looks correct...
The second column of Data1 is used in the inner loop and when Data1 is active you are restricting the range of that column with the set -bs/-es commands in the outer loop. Your script works for both worksheets if you reset col(2) to full range...
set col(2) -bs first; set col(2) -es end; sum(col(2)); set col(2) -b 1; set col(2) -e wks.maxrows;