Skip to main content

Code window

Use the Code window to write, display, and edit Visual Basic code. You can open as many Code windows as you have modules, so you can easily view the code in different forms or modules, and copy and paste between them.

You can open a Code window from:

  • The Project window, by selecting a form or module, and choosing the View Code button.
  • UserForm window, by double-clicking a control or form, choosing Code from the View menu, or pressing F7.

You can drag selected text to:

  • A different location in the current Code window.
  • Another Code window.
  • The Immediate and Watch windows.
  • The Recycle Bin.

Window elements

Element Description
Object box Displays the name of the selected object. Click the arrow to the right of the list box to display a list of all objects associated with the form.
Procedures/Events box Lists all the events recognized by Visual Basic for a form or control displayed in the Object box. When you select an event, the event procedure associated with that event name is displayed in the Code window.

If (General) is displayed in the Object box, the Procedure box lists any declarations and all of the general procedures that have been created for the form. If you are editing module code, the Procedure box lists all of the general procedures in the module. In either case, the procedure you select in the Procedure box is displayed in the Code window.

All the procedures in a module appear in a single, scrollable list that is sorted alphabetically by name. Selecting a procedure by using the drop-down list boxes at the top of the Code window moves the cursor to the first line of code in the procedure you select.

Split bar Dragging this bar down splits the Code window into two horizontal panes, each of which scrolls separately. You can then view different parts of your code at the same time. The information that appears in the Object box and Procedures/Events box applies to the code in the pane that has the focus. Dragging the bar to the top or the bottom of the window or double-clicking the bar closes a pane.
Margin Indicator bar A gray area on the left side of the Code window where margin indicators are displayed.
Procedure View icon Displays the selected procedure. Only one procedure at a time is displayed in the Code window.
Full Module View icon Displays the entire code in the module.

Keyboard shortcuts

Use the following shortcut keys to access commands in the Code window.

Description Shortcut keys
View Code window F7
View Object Browser F2
Find CTRL+F
Replace CTRL+H
Find Next F3
Find Previous SHIFT+F3
Next procedure CTRL+DOWN ARROW
Previous procedure CTRL+UP ARROW
View definition SHIFT+F2
Shift one screen down CTRL+PAGE DOWN
Shift one screen up CTRL+PAGE UP
Go to last position CTRL+SHIFT+F2
Beginning of module CTRL+HOME
End of module CTRL+END
Move one word to right CTRL+RIGHT ARROW
Move one word to left CTRL+LEFT ARROW
Move to end of line END
Move to beginning of line HOME
Undo CTRL+Z
Delete current line CTRL+Y
Delete to end of word CTRL+DELETE
Indent TAB
Outdent SHIFT+TAB
Clear all breakpoints CTRL+SHIFT+F9
View shortcut menu SHIFT+F10

Code window editing keys

Use these key combinations to edit code in the Code window.

Press To
CTRL+C Copy the selected text to the Clipboard.
CTRL+X Cut the selected text to the Clipboard.
DELETE or DEL Delete the selected text without placing it on the Clipboard.
CTRL+V Paste the Clipboard contents at the insertion point.
CTRL+Z Undo the last editing action in the current line.
CTRL+Y Cut the current line to the Clipboard.
CTRL+DELETE Delete to the end of the word.
CTRL+BACKSPACE Delete to the beginning of the word.
SHIFT+F4 Find Next: repeat text search down through your code. If no text search has been done, the Find dialog box is displayed.
SHIFT+F3 Find Previous: repeat text search up through your code. If no text search has been done, the Find dialog box is displayed.
SHIFT+TAB Remove indent.

Code window general use keys

Use these key combinations in the Code window.

Press To
F1 Get context-sensitive Help on functions, statements, methods, properties, or events.
F2 Display the Object Browser.
F9 Set or remove a breakpoint.
CTRL+SHIFT+F9 Clear all breakpoints.
F5 Run a Sub/UserForm or macro.
F8 Execute code one line at a time (single step).
SHIFT+F8 Execute code one procedure at a time (procedure step).
CTRL+BREAK Stop running a Visual Basic application.
HOME Move the cursor to the beginning of text in a line.
END Move the cursor to the end of text in a line.
Double-click on the split bar Delete the split bar.
SHIFT+F10 View shortcut menu.
ALT+F5 Runs the error handler code or returns the error to the calling procedure. Does not affect the setting for error trapping on the General tab of the Options dialog box.
ALT+F8 Steps into the error handler or returns the error to the calling procedure. Does not affect the setting for error trapping on the General tab of the Options dialog box.
CTRL+J Turn on ListProperties/Methods.
CTRL+SHIFT+J Turn on List Constants.
CTRL+I Turn on Quick Info.
CTRL+SHIFT+I Turn on Parameter Info.
CTRL+SPACEBAR Turn on Complete Word.

Code window menu shortcut keys

Use these key combinations for menu shortcuts in the Code window.

Press To
CTRL+P Print
CTRL+Z Undo
CTRL+V Paste
DEL or DELETE Delete
CTRL+F Find
SHIFT+F4 Find Next
CTRL+H Replace
TAB Indent
SHIFT+TAB Outdent
SHIFT+F2 Definition
CTRL+SHIFT+F2 Last Position
F2 Object Browser
CTRL+G Immediate window
CTRL+R Project Explorer
F4 Properties window
F8 Step Into
SHIFT+F8 Step Over
CTRL+F8 Run To Cursor
F9 Toggle Breakpoint
CTRL+SHIFT+F9 Clear All Breakpoints
F5 Run Sub/Form or Run Macro
CTRL+BREAK Break
SHIFT+F10 Shortcut menu
CTRL+J List Properties/Methods
CTRL+SHIFT+J List Constants
SHIFT+F9 Quick Watch
CTRL+M Import File
CTRL+E Export File
CTRL+I Quick Info
CTRL+SHIFT+I Parameter Info
CTRL+SPACEBAR Complete Word

Code window navigation keys

Use these key combinations to navigate in the Code window.

Press To
CTRL+F2 Move the insertion point into the Object box.
SHIFT+F2 Go to the definition of the selected procedure.
CTRL+DOWN ARROW Display the next procedure.
CTRL+UP ARROW Display the previous procedure.
PAGE DOWN Page down through the procedures in your code.
PAGE UP Page up through the procedures in your code.
CTRL+SHIFT+F2 Go back to the last position in your code.
CTRL+HOME Go to the beginning of the module.
CTRL+END Go to the end of the module.
CTRL+RIGHT ARROW Go one word to the right.
CTRL+LEFT ARROW Go one word to the left.
END Go to the end of the line.
HOME Go to the beginning of the line.
CTRL+PAGE DOWN Go to the bottom of the current procedure.
CTRL+PAGE UP Go to the top of the current procedure.
F6 Switch between Code window panes (when the window is split).

Code window and Object Browser icons

The Object Browser window and Code window contain many icons that represent classes and members. The following is a list of icons and what they represent.

This icon... Represents a...
Property
Default property
Method
Default method
Event
Constant
Module
Class module
User-defined type
Global
Library
Project
Built-in keywords and types
Enum

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>