\let\lastframenumber\theframenumber should be \edef\lastframenumber{\theframenumber} see this question on texexchange https://tex.stackexchange.com/questions/277430/debugging-in-latex-debugging-walkthrough-with-specific-example/277466#277466