There is a problem in your code. Add:
myScrollPane.HPosition = (r.x + PositionInDocument.x * r.width - PositionInScrollPane.x) / (pneMyPages.PrefWidth - vpWidth)
Log(myScrollPane.HPosition) '<-----------
You will see that it is NaN (not a number) in some cases. It happens because you divide by 0.