Error in as.environment(pos) : invalid 'pos' argument

The error message in your title it doesn't seems related to the code you are showing, also a reprex is a self contained chunck of code (including sample data) that can be directly copied and executed elsewhere
You may want to read this

Have in mind that VBA and R have very different sintaxis, so translation is not straight forward.