You are on page 1of 3

13/09/2016 Run 

Code from a Module in Excel VBA ­ EASY Excel Macros

Excel  Easy
#1 Excel tutorial on the net Excel Introduction Basics Functions Data Analysis VBA 300 Examples Ask us

Run Code from a Module Follow 19k Search Entire Site   Go

 
As a beginner to Excel VBA, you might find it difficult to decide where to put your VBA code. The Create a Macro
chapter  illustrates  how  to  run  code  by  clicking  on  a  command  button.  This  example  teaches  you  how  to  run Chapter  <   > 
code from a module.
Create a Macro
1. Open the Visual Basic Editor.

2. Click Insert, Module.
Learn more, it's easy
Swap Values

Run Code from a Module

Macro Recorder

Use Relative References

FormulaR1C1

Add a Macro to the Toolbar

Macro Security

Protect Macro

Download Excel File
run­code­from­a­module.xls

Follow Excel Easy

       

3. Create a procedure (macro) called Cyan.

Sub Cyan()

End Sub

Note: a procedure is either a sub or a function. Learn more about functions and subs here, if you like.

4. The sub changes the background color of your worksheet to cyan. To achieve this, add the following code
line.

Cells.Interior.ColorIndex = 28

Note: instead of ColorIndex number 28 (cyan), you can use any ColorIndex number.

To run the procedure, execute the following steps.

5. Click Macros.

http://www.excel­easy.com/vba/examples/run­code­from­a­module.html 1/3
13/09/2016 Run Code from a Module in Excel VBA ­ EASY Excel Macros

6. Select Cyan and click Run.

Result:

Note: code placed into a module is available to the whole workbook. That means, you can select Sheet2 or
Sheet3 and change the background color of these sheets as well. The Add  a  Macro  to  the  Toolbar program
illustrates  how  to  make  a  macro  available  to  all  your  workbooks  (Excel  files).  Remember,  code  placed  on  a
sheet (assigned to a command button) is only available for that particular sheet.

http://www.excel­easy.com/vba/examples/run­code­from­a­module.html 2/3
13/09/2016 Run Code from a Module in Excel VBA ­ EASY Excel Macros

Micromax X1800 (Black) Intex Eco Plus …
Rs. 722.00   Rs. 712.00  
(details + delivery) (details + delivery)

Do you like this free website? Please share this page on Google+
Share 7

3/9 Completed! Learn more about creating macros >
Go to Top: Run Code from a Module    |    Go to Next Chapter: MsgBox

COPYRIGHT (C) 2010­2016 WWW.EXCEL­EASY.COM. ALL RIGHTS RESERVED.
EXCEL 2010 TUTORIAL | HOW TO USE EXCEL | MICROSOFT EXCEL 2010 | VBA IN EXCEL

http://www.excel­easy.com/vba/examples/run­code­from­a­module.html 3/3

You might also like