VBA – Insert Into String

Inserting into string in Excel should be a trivial task, but it always makes sense to have something prepared in the boilerplate, before you start working.

This is my 2. article with that cat, it is becoming famous

Ok, why is it important to have a nice function, if something like this already does the job:

Good question, and the answer is because we will not always remember to add some nice corner cases. E.g. what would happen if we want to insert on a position, that is way bigger than the length of the string like this:

Or if we somehow passed a parameter, and it turned out to insert a string at the 0-th or a negative position, as it somehow makes sense for the author?

Ok, without further delay, the code handling these two cornercases is here:

And if you feel like improving my boilerplate, you might consider doing it here: https://github.com/Vitosh/VBA_personal/blob/master/Formatting/InsertIntoString.vb

Enjoy! 🙂