

This method permits precise naming of a specific workbook, thereby ensuring it’s exactly the workbook you want to save. It’s far more robust to use “pure VBA” to save the workbook with a Workbook.Save. I’d strongly recommend against using SendKeys to do this, though.

If you want to save a workbook after making modifications via some subroutine, it’s theoretically possible to send the Ctrl+s keyboard shortcut to save. Technically, you can use SendKeys to send keyboard shortcuts to MS Office programs. We’ll talk more about how to do that in a bit.

If you want to send keystrokes to another program, you’ll need to activate it.

If the workbook is your target, run your code from the Macros selection box in the GUI. It’s imperative to first activate whatever element you want SendKeys to impact. You’ll end up with something like this, which probably is not what you intended:ĭespite you trying to activate Cell A1, the VBE was still active at the time of running, so SendKeys placed the keystrokes at the cursor in the VBE instead of in your desired Cell. To illustrate: open the VBE, input this code, and run the macro with F5. In fact, you can’t even run some bits of code from the VBA Editor, as the VBE will still be active! Thus, you cannot step through your code to verify its correctness. Moreover, the active element at exactly the time SendKeys executes receives the keystrokes. If the wrong window is active, you’ll be sending keystrokes to the wrong window. Ideally this is the place you want the keystrokes to impact, such as a cell in a worksheet, an open notepad, or a browser URL bar. SendKeys literally sends the indicated keystrokes to whatever element is active. Many tutorials start off with a warning on using SendKeys, and they have a good reason.
