Yes, you can start Notepad directly from Excel VBA using the Shell function. This automates opening a blank Notepad window—ideal for logging output, writing quick notes, or integrating text editing into a larger VBA workflow. No extra tools are required beyond Excel itself (with VBA enabled) and Windows, as Notepad is a built-in Windows application.
What You'll Need
- Microsoft Excel installed (Excel 2010 or later recommended, but works in most versions)
- VBA enabled in Excel (macro security set to allow macros)
- Windows operating system (Notepad is Windows-native; this does not work on macOS)
Step-by-Step
- Open Excel and create a new blank workbook (or use an existing one where you want to add the feature).
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, go to Insert > Module to add a new code module to your workbook.
- Copy and paste the following VBA code into the module window:
Sub StartNotepad() Dim notepadPath As String notepadPath = "notepad.exe" Shell notepadPath, vbNormalFocus End SubThis code defines a subroutine namedStartNotepad, sets the target executable tonotepad.exe, and uses theShellfunction to launch it with normal window focus. - Close the VBA editor and return to Excel.
- Press Alt + F8 to open the Macro dialog box.
- Select StartNotepad from the list and click Run.
- Notepad should open immediately. If it does not, check the troubleshooting section below.
Common Mistakes
- Typing
notepadwithout the.exeextension: While Windows often resolves this automatically, VBA'sShellfunction is strict about full paths or executable names. Always include.exeto ensure reliability. - Running on macOS: Notepad is exclusive to Windows. This method will fail silently or return an error on non-Windows systems.
- Macro security blocking execution: If macros are disabled, the code will not run. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros (temporarily, for testing only), then re-enable notifications for production use.
- Not declaring the variable: Omitting
Dim notepadPath As Stringcan cause runtime errors ifOption Explicitis used at the top of the module. Always declare variables or removeOption Explicitif you prefer implicit declaration (not recommended for production). - Using incorrect capitalization: VBA is case-insensitive, but mixing case in paths (e.g.,
Notepad.exevs.notepad.exe) can cause confusion when debugging. Stick to lowercase for Windows executables.
Tips
- Open Notepad with a file: Modify the
Shellline to open a specific file, likeShell "notepad.exe C:\temp\log.txt", vbNormalFocus. Ensure the file path uses double backslashes (\\) or forward slashes (/) to avoid escape-character issues. - Wait for Notepad to close: The
Shellfunction starts Notepad asynchronously. To wait until the user closes Notepad, use Windows API calls (e.g.,CreateProcessandWaitForSingleObject), but this adds complexity and is rarely needed for simple text editing. - Use the full path for reliability: If Notepad is missing from the system PATH (rare), use the full path:
Shell "C:\Windows\System32\notepad.exe", vbNormalFocus. - Test in a new workbook first: Avoid corrupting a live project by testing new macros in a blank workbook before adding them to critical files.
- Save as .xlsm: Always save your Excel file as Excel Macro-Enabled Workbook (*.xlsm) to preserve VBA code. Saving as .xlsx strips macros.
Troubleshooting
If Notepad does not open when you run the macro, follow these steps in order:
- Verify macro execution: After pressing Alt + F8, ensure
StartNotepadappears in the macro list. If not, re-paste the code and confirm it is inside aSub ... End Subblock in a standard module (not inThisWorkbookor a worksheet module). - Check for runtime errors: Press F5 while in the VBA editor to run the macro directly. If an error appears (e.g., "File not found"), double-check the
notepadPathstring and ensure it exactly matches"notepad.exe". - Confirm Windows Notepad exists: Press Win + R, type
notepad, and press Enter. If Notepad does not open, your system may be corrupted. Reinstall Notepad via Settings > Apps > Optional Features, or runsfc /scannowin Command Prompt as Administrator. - Check macro security: In Excel, go to Developer > Macro Security (if the Developer tab is hidden, enable it via File > Options > Customize Ribbon). Set it to Enable all macros temporarily. If it works after this change, adjust security to Disable all macros with notification and digitally sign your macro later.
- Run Excel as Administrator: Some corporate or locked-down systems block shell commands. Right-click Excel, select Run as administrator, and try again. If it works, your user account lacks permissions to launch external processes.
- Antivirus interference: Security software may block
Shellcalls. Temporarily disable real-time protection (if safe to do so) and test. If successful, add an exclusion for Excel in your antivirus settings.
Frequently Asked Questions
Does this work on Excel for Mac?
No. Notepad is a Windows-only application. On macOS, you can use the equivalentTextEdit app by changing the path to "/Applications/TextEdit.app", but this requires AppleScript and is not compatible with Windows Excel. Cross-platform automation is not supported natively.
Can I pass text to Notepad when it opens?
Not directly viaShell. You can write text to a temporary file first, then open that file in Notepad: Shell "notepad.exe C:\temp\temp.txt", vbNormalFocus, where temp.txt contains your desired content. Use VBA's Open statement to create and write to the file before launching Notepad.
Why does Notepad open minimized or hidden?
TheShell function uses the second argument to control window state. vbNormalFocus restores and focuses the window. Use vbMinimizedFocus for minimized, vbMaximizedFocus for maximized, or vbHide to hide it. If you see no window, check your antivirus or task manager—the process may be running but not visible.
How do I close Notepad automatically after a delay?
VBA cannot directly close external apps like Notepad after a delay without Windows API calls. A workaround is to open Notepad with a temporary file, write your content, then useApplication.OnTime to schedule a macro that sends Alt + F4 via SendKeys. However, SendKeys is unreliable and can send keystrokes to the wrong window. Use only in controlled environments.
Can I launch Notepad with a specific font or size?
No. Notepad does not support command-line arguments for font or window size. You must open Notepad and adjust settings manually, or use a different text editor (like VS Code or Notepad++) that supports CLI arguments.Is there a security risk in using Shell to launch Notepad?
Minimal risk if you hardcode"notepad.exe". The danger arises if you pass user input directly into the Shell command (e.g., Shell InputBox("Enter path")), which could execute malicious commands. Always validate and sanitize inputs, or avoid dynamic paths entirely for simple tasks like this.