How to Duplicate a Line of Code in VBA
In yesterday's article, I lamented the arduousness inherent in duplicating lines of code in VBA.
To work around VBA's lack of a duplicate line shortcut key, I use the following keystroke sequence instead:
- [Shift]+[Down] to make my initial selection
- [Ctrl]+[C] to copy the selection
- [Ctrl]+[V] to move the cursor to the end of the selection
- [Ctrl]+[V] to paste the selected text
As I wrote yesterday, I've used this technique over the years to save literally dozens of seconds writing code every single month.
An Even Better Way
I can't can begin to express the joy I felt when I checked the comments on that article this morning, but I've not the time nor the vocabulary to express that joy in full.
Lo, in the comments, esteemed reader Carsten Gromberg presented a much more efficient alternative than mine.
Behold, the much more efficient alternative:
- [Ctrl] + [Y] -> deletes the complete line to the clipboard (the cursor can be anywhere in this line)
- [Ctrl] + [V] -> restores the line
- [Ctrl] + [V] -> inserts a copy of that line after the restored line
I will point out that this technique does not work to copy multiple lines of code at once, but for single-line code copying it puts my own efforts to shame.
Vielen Dank, Carsten!
And Richard Rost, you'll need to stay up late if you want to beat those Europeans to the punch next time.