How to Duplicate a Line of Code in VBA

It's not quite a single shortcut key, but it's nearly as efficient.

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:

  1. [Shift]+[Down] to make my initial selection
  2. [Ctrl]+[C] to copy the selection
  3. [Ctrl]+[V] to move the cursor to the end of the selection
  4. [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:

  1. [Ctrl] + [Y] -> deletes the complete line to the clipboard (the cursor can be anywhere in this line)
  2. [Ctrl] + [V] -> restores the line
  3. [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.

All original code samples by Mike Wolfe are licensed under CC BY 4.0