Fast document navigation
Another practical use of VBA is to create placeholders you can quickly set and then return to later. This is especially useful for those big labyrinthine files you tend to get lost in.
Try this example for setting placeholders (AKA bookmarks) with VBA:
- Inside Word, press alt-F11 to enter the Visual Basic IDE and navigate to the current document's code window, as you did with the previous example.
- Paste the following code into the code window:
Public Sub setBmark()
ActiveDocument.Bookmarks.Add Name:="mybmark", Range:=Selection.Range
Selection.GoTo What:=wdGoToBookmark, Name:="mybmark"
Return to Word and assign shut keys to both macros: press Office Button>Word options>Customize, then click the Customize button near the window's bottom. Enter an unused keyboard combination in the dialog box and press OK.
Try out your bookmark macros: open a document, press the shortcut key for the setBMark macro, navigate to another part of the document, then press the shortcut key for the gotoBmark macro. Word will return you to the set bookmark.
Bookmarks for Excel
Excel doesn't have Word's bookmarks per se, but something similar you can adapt the navigation macros to: names. Here's how to use VBA to create and return to placeholders using Excel's Names:
Open an Excel workbook and enter the VB IDE's code window using the instructions from the previous examples. Paste the following code into the code window:
ActiveWorkbook.Names.Add Name:="_bmark", RefersTo:=Selection
Read more about assigning names to Excel's ranges here.
Set Excel's shortcut keys
Return to Excel and assign shortcut keys to both macros: press alt-F8, select the setName macro from the list, then press Options. Enter a memorable shortcut key (which must be used together with the control key). Press OK, then repeat this step to assign a new shortcut key to the gotoName macro.
Try out your macros by pressing the shortcut key for setName, navigating to another cell, then pressing the shortcut key for gotoName. Excel will return you to the placeholder you set with setName.