How Do I Create a Microsoft Excel Script?

Save

Microsoft Excel supports automating Excel commands and features using macros and Visual Basic for Applications scripting. Macros are a pre-recorded series of commands, set to run automatically when a specific command is given. VBA is a dialect of the Visual Basic programming language and can be used to create macros, as well as to execute automated commands based on certain conditions.

Enabling Macros and Scripts

Before you can get started creating macros and VBA scripts, enable the Developer tab on the Ribbon menu. The Developer tab is where the option to create macros and scripts is kept, as well as including other tools designed for advanced Excel users and developers. Once the option is enabled, adjust Excel's macro security setting so you can test and run your macros.

Step 1

(Image: Image courtesy of Microsoft.)

Click File.

Step 2

(Image: Image courtesy of Microsoft.)

Select Options.

Step 3

(Image: Image courtesy of Microsoft.)

Click Customize Ribbon and tick the Developer check-box.

Step 4

(Image: Image courtesy of Microsoft.)

Click OK.

Step 5

(Image: Image courtesy of Microsoft.)

Click the Developer tab in the Ribbon menu.

Step 6

(Image: Image courtesy of Microsoft.)

Click Macro Security.

Step 7

(Image: Image courtesy of Microsoft.)

Select the Enable all macros (not recommended; potentially dangerous code can run) radio button and click OK.

Tip

  • When you're finished using your macros and scripts, return to this menu and set the option to either Disable all macros with notification or Disable all macros except digitally signed macros to prevent potentially malicious code from running in other Excel documents.

Creating Macros

Macros automatically run through their recorded commands and actions when a specific command is issued. Every action you take in Excel during recording is added to the macro, so do a couple of practice runs to smooth out your process before you begin recording.

Step 1

(Image: Image courtesy of Microsoft.)

Click Record Macro from the Developer tab.

Step 2

(Image: Image courtesy of Microsoft.)

Enter a name, shortcut and description for your macro in the appropriate fields.

Tip

    • Since your macro triggers using the Ctrl key and the key you specify, choose a key combination that you don't normally use or that isn't associated with a function built into Excel.
    • Macro names must start with a letter, cannot contain spaces, and the shortcut key must be a letter.

Step 3

(Image: Image courtesy of Microsoft.)

Click the Store macro in drop-down and select where you want to keep the macro.

  • This Workbook limits the macro to the Excel document you're currently editing.
  • New Workbook creates a new Excel document specifically for storing this macro.
  • Personal Macro Workbook creates a hidden Excel document that stores all your macros for use with any Excel document.

Step 4

(Image: Image courtesy of Microsoft.)

Click OK.

Step 5

(Image: Image courtesy of Microsoft.)

Perform the actions you want to automate, and then click Stop Recording. Use the keyboard shortcut specified to use your new macro.

Using VBA Scripting

VBA takes some practice to understand and apply properly, so if you're new to the language, check out guides like Excel Easy and Home & Learn's Excel VBA courses and tutorials. Microsoft's Hey, Scripting Guy! blog series also contains some useful examples of scripts for Excel.

Step 1

(Image: Image courtesy of Microsoft.)

Click Visual Basic from the Developer tab.

Step 2

(Image: Image courtesy of Microsoft.)

Find the sheet you want to add a VBA script to from the Project - VBAProject pane and double-click the sheet.

Step 3

(Image: Image courtesy of Microsoft.)

Type out or paste the VBA code segment you want to add to your sheet into the Code window.

Step 4

(Image: Image courtesy of Microsoft.)

Click Run followed by Run Sub/UserForm to test your VBA script.

Tip

  • The F5 key also runs your script.

Step 5

(Image: Image courtesy of Microsoft.)

If the script works as intended, continue with any further scripting or work with Excel. If not, adjust the script in the Code window until it behaves as intended.

Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!