This is not your whole code. But I can see that I causes the problem. I must be not greater than the length of String 1. So if Str1 is e.g. 'Hello World' and I is 19, although the length is 11, it will cause an invalid dim.
Tip: I must be an integer between 1 and the length of the string.
If you cant control I and dont know which number I is, you can prevent the Error by:
Also Str1 must be not archived.
Why does the code above prevent an dim error, when I is invalid?
The min( prevents a too high I. If I is higher than allowed, min( automatically takes the highest allowed number.
The abs( prevents the use of negative numbers and the int( prevents the use of floats.
I hope this helps and welcome to this forum :)