Expand / contract the selected VBA code
- John
- Apr 29
- 3 min read
On occasion, I need to select a word, line or 'block' of code. This normally involves moving the selection to the start (or end) of the desired selection then holding down the shift key while either using the mouse to select the end (or start) of the selection or using the arrow keys on the keyboard.
Sometimes, I'm doing this because I want to copy the word, line or 'block'. Sometimes I want to delete it. Whatever, doesn't matter. The same selection process is always involved ... until now. VBE_Extras now provides commands to 'Expand selection' and 'Contract selection'. You'll find these commands in the 'Goto' sub-menu of the main VBE_Extras menu.
However, much more handy is to use keyboard shortcuts for these commands. By default (you can customise these keyboard shortcuts if you wish ... or switch them off ... in the VBE_Extras Settings), 'Expand selection' uses Alt+] and 'Contract selection' uses Alt+[. You can also add these commands to the VBE_Extras Toolbar if you want.
Here's an example of 'Expand selection' and 'Contract selection' in action.

As you can see, the selection initially starts off as an insertion point within the "Preserve" word. I then use the 'Expand selection' keyboard shortcut repeatedly to:
First, select the word that the selection is within i.e. "Preserve" (if the initial selection had been in a code comment then the first 'expand' would have been to select the entire code comment)
Then, select the line that the word is within (if the line was part of a longer logical line made up of multiple physical lines then the next selection would have been the entire logical line ... but, in this case, the line is just a plain-old-line with no continuations)
Then, select the 'block' that the line is within (in this case, that's the block starting from 'If' and ending with 'End If')
Then, select other lines at the same "level" within the parent block if any exist ... in this case they do ... the line starting with 'GetFilesInFolder' before the 'If' line and the line starting with 'outasFiles' after the 'End If' line
Then, select the next block, another 'If' to 'End If' in this case
And so on up through the next 'If' to 'End If', then a 'For' to 'Next', then another 'If' to 'End If', a 'With' to 'End With', then the entire procedure from 'Function' to 'End Function' and finally all other lines at the same "level" (that is the blank and comment lines before the 'Function' line and the blank line after the 'End Function' line).
Then, 'Contract selection' keyboard shortcut is used to step backwards all the way back down from the Function, the various blocks, the line, the word and back to the original insertion point.
Why bother with this? I find this useful for a few reasons:
Sometimes, when it is an insertion point, I've just plain-old lost sight of where the selection is (I'm getting old, eyesight must be failing ...) and expanding the selection to a word and then back to an insertion point identifies this immediately
Sometimes, when reading over code written previously, it's helpful to quickly see where a block of code starts and ends ... this is especially the case when reading someone else's code that isn't indented correctly (or, of course, you can also use VBE_Extras to fix that for you also ... see the Automatically indenting VBA code post)
Sometimes, I want to select a line because I'm going to delete it or maybe more it up or down (though VBE_Extras has another option for that also ... see the Moves lines up or down post)
Sometimes, I want to select an entire procedure because I'm going to move it elsewhere ... so just keep pressing 'Expand selection' until it expands no more ... then you can cut or delete or whatever you need.
Comments