- Microsoft Excel Tutorials Free
- Excel 2008 Tutorial For Mac Free Download
- Excel For Mac Tutorial Free
- Microsoft Excel For Mac Tutorial
for Microsoft Office Excel, Access & Word on Windows or Mac
Excel includes custom lists that you can sort by: days of the week and months of the year. In addition, you can create your own custom lists, for example, grade levels in a school, such as Freshman, Sophomore, Junior, and Senior. On the Excel menu, click Preferences, and then under Formulas and Lists, click Custom Lists. The course is compatible with Microsoft 365 (previously known as Office 365) and Excel for Windows 2019, 2016, 2013, 2010. Also available: Excel for Mac. Excel’s formula structure hasn’t changed since Excel 95, so the functions and formulas from the course can be used with almost any version of Excel. PivotTable tutorial. In this PivotTable tutorial template, we’ll show you how to create and use a variety of PivotTables in Excel. This Excel PivotTable tutorial template teaches you when it's appropriate to make a PivotTable. Get an idea of the basic parts of a PivotTable with this user-friendly PivotTable tutorial template. O For Mac users, the random functions are in the following add-in: Random Functions for the Mac.xlam. Excel tutorial: Here is a free version of my Excel tutorial: Excel Tutorial for Windows.xlsx. It provides information for upgrading to a more complete version called ExcelNow! Video tutorials are recorded in Microsoft Excel 2016 for PC. Learn more about how a GoSkills Excel certification can boost your career. Want to be a more efficient Excel user? Start learning 200 of the best Excel shortcuts for PC and Mac. Once enrolled, our friendly support team and tutors are here to help with any course related inquiries.
IDAutomation VBA Barcode Functions and Macros allow easy generation of barcodes in Microsoft Office Suite applications such as Excel, Word and Access on both Windows® and Mac operating systems, compatible with Excel 2003 and greater in Windows and Excel 2011 and greater on Mac. The VBA modules are also compatible with legacy VB6.
These font encoders are used to format the
- Charles Daneri, Objective Systems, LLC (computer consulting firm), Baltimore, MD.
These Office Macros consist of pure VBA modules with customizable code, functions and other options that allow for dynamic and variable data barcode generation. Consequently, this also means that the implementation may be more complicated for the average user. These font encoder tools format data into a text string that will display a readable barcode when the appropriate IDAutomation font is applied to it.
The Macros are free to use with the purchase of any IDAutomation linear barcode font and are compatible with Microsoft Office 2000 and greater running on Windows® or Office 2004 and 2011 for Mac platforms. This tool is not compatible with Office 2008 for Mac because that version does not have VBA capability. IDAutomation has a variety of Mac Barcode Integration options for those using Microsoft Office 2008 for Mac. Mac users and those located outside of the US Western character set should use the IDAutomation Universal Barcode Font Advantage package with related Universal Font VBA functions for Code 128 and Interleaved 2 of 5 fonts.
Importing and Exporting VBA in Excel
The barcode Macros and functions reside inside the Excel file as a custom VBA module. To use these barcode functions in an Excel spreadsheet, the IDAutomationVBAmodule must be imported. If the IDAutomationVBA.bas file is not available for import, it may be exported from the sample spreadsheet provided in the downloaded file. With all 2D font packages, such as QR-Code and
- Thomas Bell, Cynosure, Westford, MA
How to Create a Barcode in Excel
- Enable the security setting that allows Macros to run. If prompted when the document re-opens, choose Enable Macros.
- Office 2007 and above applications require the Macros to run from a trusted location.
- Office 2004 and 2011 for Mac settings are located at Excel - Preferences - Security - Macro Security.
- Excel 2000 and 2003 require a medium-security setting to run Macros. Open Excel and set the security level to medium by choosing Tools - Macro - Security.
- Signed Macros are included in the latest version of the VBA Module download. Simply choose the Trust this Publisher option and the VBA Macros will run unprompted. Data may be pasted into these files and used if the Macros are not modified.
- If a different spreadsheet is used other than the sample provided, the Macros must be imported into the spreadsheet.
- Create a column in the spreadsheet for the barcode.
- Size the column to make sure it is wide enough to contain the entire barcode.
- Format the column so that any text appearing in it will be centered. This is necessary to create the white space (often called the quiet zone) before and after the barcode.
- Enter the formula in this cell that will format the data to the barcode font. If unsure which function or barcode to use, consider the Code 128 barcode fonts with the =Code128(B10,0) function where 'B10' refers to the cell location of the data that is to be encoded and the ',0' formats the result of the formula to the Code 128 fonts. For all 2D fonts, refer to the documentation within that particular package.
- Examine the spreadsheet to make sure the data is being properly formatted to the barcode font. Strange characters may append to the beginning and end of the data from the fields, but this is normal. In some cases, the data may need to be reformatted and may appear scrambled. This is normal for Code 128 and Interleaved 2 of 5 barcode fonts when numbers need to be compressed within the barcode.
- After verifying that the text in the cell has been formatted correctly, select the appropriate font and set the point size to 12. This example selects the IDAutomationC128XS font. The XS and S sizes of IDAutomation's fonts in Code 128 and Code 39 are specifically designed to format correctly in Excel, as larger fonts will not usually format correctly in the cell. 2D symbols within Excel directly are only possible with the 2D XLS font for Excel.
- Ensure that the column is wide enough to contain the entire barcode with some white space before and after the barcode.
- Scan the printed barcodes with a handheld USB barcode scanner to verify the correct data is encoded.
Symbology Specific Tutorials for Excel:
2D Barcodes:
Linear Barcodes:
Barcoding an Entire Column in Excel
IDAutomation's barcode fonts and Macros provide an easy method of barcoding a column in Excel. In this example, Code 128 barcodes are created in Column B from data in Column A. This example assumes the Code 128 Barcode Font Package has been installed, and the VBA module has been imported into the spreadsheet.
- Enter the formula in one cell to format the data to the barcode font as explained in the Barcode Tutorial for Excel:
- Select that cell and choose Edit - Copy.
- At this point, it may be necessary to deselect any cells not needed for barcoding. Then, highlight an entire column by selecting the gray square letter-labeled area at the top of the spreadsheet and choose Edit - Paste. In this example, the button is marked B. If the spreadsheet is large, the formulas may take some time to paste and re-calculate if the spreadsheet is large.
- Change the column to the appropriate barcode font. In this example, choose the IDAutomationC128XS font while the entire column that contains the formula is selected. The barcodes will appear in the entire column. When displaying 2D barcodes in Excel such as QR-Code, Data Matrix or PDF417, use the 2D XLS Font specifically designed for Excel at 8 points.
- Confirm that the barcodes are accurate by scanning them from printed sheets, or directly from the screen. IDAutomation offers a variety of barcode readers that can perform this function with ease.
Video Tutorials
- Video: How to Create a Barcode in Excel
- Video: How to Create a Barcode in Excel 2007 and above
- Video: How to Create a Barcode on Mac Excel 2004
Access VBA Macro Barcode Functions Tutorial
After the appropriate files have been downloaded and extracted, open the sample Access database provided. This database contains one table, one module, and some sample reports. The module contains custom VBA code, functions, and Macros. The sample database should be used as a reference if help is needed.
How to Create Barcodes in an Access Report
Before starting this tutorial, ensure that VBA Macros are the desired implementation. Several methods of creating barcodes in Access are explained in the Barcode Integration Guide for Microsoft Access.
- If a different database is being used and not the sample provided, first import the VBA Macros into the database.
- The most recent versions of Access require a medium security setting to run Macros.
- Open Access and set the security level to medium by choosing Tools - Macro - Security.
- If prompted when the document is reopened, select Enable Macros.
Office 2007 and above may require that the Macros run from a trusted location. Signed Macros are included in the latest version of the VBA download. Simply choose the Trust this Publisher option and the VBA Macros will run unprompted. Tables, Reports, Forms and other information may be pasted into these files and used if the Macros are not modified.
- Open a report in Design Mode.
- Create a text box where the barcode will display.
- To format the data to the desired barcode font in a text box, a function must be placed in the Control Source specifying the data field that needs to be encoded.
- The formula =function([field]) should be entered in the Control Source property of the text box, for example:
- In some cases, it may be necessary to specify the table and field in the control source, for example: If unsure of which function to use, consider the =Code128([field]) function with Code 128 Barcode Fonts.
- Optional: The Control Source may be changed to a formula that appends text to the data or combines multiple fields, for example: or or
- In Code 128, the ApplyTilde feature may also be used to encode tab and return functions. For example, the following formula creates a tab function between two fields:
- Run the report to make sure the data is being populated from the fields and formatted to the barcode font. Strange characters may append to the beginning and end of the data, but this is normal. In some cases, the data may need to be reformatted and it may appear to be scrambled. This is normal for Code 128, Interleaved 2 of 5, DataBar and 2D barcodes.
- Change the formula field font to the appropriate barcode font and set the point size to 12. If this step is not followed, generated barcodes will not display.
- Print and scan the barcodes to verify that the correct data is encoded. If a scanner is needed to verify barcodes, consider the IDAutomation USB Barcode Scanner.
Note: When distributing Access Databases, the associated barcode font must be installed on each computer that prints the barcodes. If this is inconvenient, the Native Barcode Generator for Access is a complete barcode generator object that stays embedded in the database, which means no fonts need to be installed on user computers. For more information about other methods of barcoding in Access, please refer to the Microsoft Access Barcode Integration Guide.
If a large amount of data needs to be encoded, the PDF417 Barcode Font and Encoder or the Data Matrix Barcode Font and Encoder may be the best options as these 2D barcode types allow for the encoding of more data.
Video Tutorial Create a Barcode in Access 2010 and above
VBA & Macro Functions in Microsoft Word Mail-Merge
Before starting this barcode tutorial, ensure VBA Macros are the desired implementation for a Word mail-merge. An easier method exists when using Codabar or Code 39 fonts, and this is explained further in the Barcode Integration Guide for Microsoft Word.
Excel must be used as the data source when creating barcodes in a Microsoft Word mail merge using Macros. The field used for the data source in Word should be the column in Excel where the formula has been applied, which is used to format the data to the barcode font. It is best to use the last column of the spreadsheet in order to avoid merging conflicts.
To set up an Excel spreadsheet with these barcode Macros, refer to the Barcode Tutorial for Excel.
Note: C128 ReturnTypes 6 through 9 are not currently supported in Excel-Word mail-merges.
- Excel 2000 and 2003 require a medium security setting to run Macros. Open Excel and set the security level to medium by choosing Tools - Macro - Security.
Office 2007 and above may require the Macros to run from a trusted location.
In Office 2004 for Mac, go to Excel - Preferences - Security and select Macro Security to enable the secure setting. - After the files have been downloaded and extracted, open the included Word Mail-Merge document. A message may appear stating that the data source cannot be found. If so, select Data Source for Word Mail Merge.xls as the data source. If asked, enable Macros.
- The formula may be viewed in the cells of the Barcode column in the Excel spreadsheet. This formula retrieves the data to encode and passes it to the appropriate barcode function. The function will then format the data to the barcode font. This column is then passed to Word when the mail merge is started. The appropriate barcode font must be selected for the field in MS Word; in this case, it is «Barcode».
- The barcode will not look correct and will not scan until the mail merge is performed.
- After the merge is performed, print and scan the barcode to verify that the correct data has been encoded. If a scanner is needed to verify barcodes, IDAutomation suggests easy-to-use USB Barcode Scanners.
- When distributing the mail-merge, the font should be embedded in Word and the Excel data source must be accessible.
VBA Barcode Macro Functions and Properties
The functions listed below are available in IDAutomation's IDAutomationVBA.bas file and are only valid when used with the font listed in the 'font to use' column. Functions beginning with IDAutomation_Uni_ may only be used with the IDAutomation Universal Barcode Font Advantage™ package and are supplied in the IDAutomationWindowsDLLVBA.bas file provided within that package.
DataToEncode is the string data type for all functions listed below. Other data types such as numbers or dates may need to be converted to the string data type to be properly encoded. Additional parameters are available for some functions and are optional. For example, Code128('123456',0,True). For UPC-A, UPC-E & EAN-13, the +2 and +5 add-on codes may be created by adding the digits to the end of the data being encoded.
VBA Functions for Standard Barcode Fonts | |||
Barcode Type | Barcode Function Methods | Notes | VBA File to Import | Required Font |
Code-11 | Code11 (DataToEncode) | IDAutomationVBA.bas | IDAutomationC11 |
Code-128 (Auto Mode) | Code128 (DataToEncode, C128 ReturnType, ApplyTilde) Human Readable text is enabled when ReturnType = 6 Example: Code128('123456789012', 6, 0) | IDAutomationVBA.bas or IDAutomation VBA for Code 128 and Postnet.bas* | IDAutomationC128 |
Code-128 (Manual Mode) | IDAutomation recommends using Auto Mode for most situations. Code128a (DataToEncode, C128 ReturnType) Code128b (DataToEncode, C128 ReturnType) Code128c (DataToEncode, C128 ReturnType) Example: Code128b('12345678', 6) | IDAutomationVBA.bas or IDAutomation VBA for Code 128 and Postnet.bas* | IDAutomationC128 |
Code-39 | Code39 (DataToEncode) Code39Mod43 (DataToEncode, ReturnType) Example: Code39Mod43('12345678', 0) | IDAutomationVBA.bas | IDAutomationC39 |
Code-93 | Code93 (DataToEncode) | IDAutomationVBA.bas | IDAutomationC93 |
Codabar | Codabar (DataToEncode) | IDAutomationVBA.bas | IDAutomationCB |
EAN-13 | IDAEAN13 (DataToEncode) | IDAutomationVBA.bas | IDAutomationUPCEAN |
EAN-8 | IDAEAN8 (DataToEncode) | IDAutomationVBA.bas | IDAutomationUPCEAN |
GS1-128 | Code128 (DataToEncode, C128 ReturnType, 1) GS1-128 is enabled in Code 128 Auto by setting ApplyTilde to True. Human Readable AIs may be created by setting the C128 ReturnType to 6. Example: Code128('(12)3456789012', 6, 1) | IDAutomationVBA.bas or IDAutomation VBA for Code 128 and Postnet.bas* | IDAutomationC128 |
Interleaved 2 of 5 | I2of5 (DataToEncode) I2of5Mod10 (DataToEncode, ReturnType) Example: I2of5Mod10('123456789', 1) NOTE: ReturnType 3 generates an OPC (Optical Product Code) symbol. OPC is also known as Vision Council of America OPC, VCA BarCode & VCA OPC.Example: I2of5Mod10('020748721', 3) | IDAutomationVBA.bas | IDAutomationI25 |
MSI / Plessey | MSI (DataToEncode, ReturnType) | IDAutomationVBA.bas | IDAutomationMSI |
RM4SCC | RM4SCC (DataToEncode) | IDAutomationVBA.bas | IDAutomationRM |
UPC-A | UPCa (DataToEncode) | IDAutomationVBA.bas | IDAutomationUPCEAN |
UPC-E | UPCe (DataToEncode) | IDAutomationVBA.bas | IDAutomationUPCEAN |
USPS IntelligentMail (IMb) | IntelligentMail (DataToEncode) ** Included only in the purchased version of the USPS IMb Font Package. | IDAutomation VBA for Intelligent Mail.bas | IDAutomationPOSTNET or IDAutomationIMB |
USPS Postnet | Postnet (DataToEncode, ReturnType) *Included in Postnet package. | IDAutomationVBA.bas or IDAutomation VBA for Code 128 and Postnet.bas* | IDAutomationPOSTNET |
USPS Planet | Planet (DataToEncode, ReturnType) | IDAutomationVBA.bas or IDAutomation VBA for Code 128 and Postnet.bas* | IDAutomationPOSTNET |
not applicable | MOD10 (DataToEncode) | IDAutomationVBA.bas | not applicable |
not applicable | SpliceText (DataToEncode, SpacingNumber, ApplyTilde) | IDAutomationVBA.bas | not applicable |
Functions for the Universal Barcode Font | |||
All functions listed below are preceded with IDAutomation_Uni_.* They may only be used with the IDAutomation Universal Barcode Font Advantage™ and are supplied in the IDAutomation_UniversalFont_VBA.bas or the IDAutomationWindowsDLLVBA.bas file which also requires the installation of the product EXE file in order for the macro to function, because they access an installed IDAutomationNativeFontEncoder.dll that is provided within the package. Note: Excel 2004 for Mac requires the functions to be lowercase. Ex: idautomation_uni_c128 | |||
Barcode Type | Universal Function Methods | Notes | VBA File to Import | Required Font |
Code-128 | IDAutomation_Uni_C128 (DataToEncode, ApplyTilde) Note: Code128() is the recommended method to use. Code128() is also used to create GS1-128. IDAutomation_Uni_C128A (DataToEncode) IDAutomation_Uni_C128B (DataToEncode) IDAutomation_Uni_C128C (DataToEncode) Example: IDAutomation_Uni_C128 ('Ê8100712345Ê2112WH5678', TRUE) Excel for Mac: idautomation_uni_c128 ('Ê8100712345Ê2112WH5678', TRUE) | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation_Uni |
not applicable | C128HR (DataToEncode, ApplyTilde) Returns text for Code 128 barcodes, such as for GS1-128. | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | Text Font |
Code-39 | IDAutomation_Uni_C39 (DataToEncode, N_Dimension, IncludeCheckDigit) Example: IDAutomation_Uni_C39 ('123456789', 3, TRUE ) Excel for Mac: idautomation_uni_c39 ('123456789', 3, TRUE ) | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation_Uni |
Codabar | IDAutomation_Uni_Codabar (DataToEncode, N_Dimension, StartChar, StopChar) Example: IDAutomation_Uni_Codabar ('123456789', 3,'A','B' ) Excel for Mac: idautomation_uni_codabar ('123456789', 3,'A','B' ) | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation_Uni |
Interleaved 2 of 5 | IDAutomation_Uni_I2of5 (DataToEncode, N_Dimension, IncludeCheckDigit) | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation_Uni |
MSI / Plessey | IDAutomation_Uni_MSI (DataToEncode, N_Dimension, IncludeCheckDigit) | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation_Uni |
USPS IntelligentMail | IDAutomation_Uni_IntelligentMail (DataToEncode) ** | IDAutomation_UniversalFont_USPS_IMb_VBA.bas | IDAutomation_Uni |
USPS Postnet | IDAutomation_Uni_Postnet (DataToEncode, IncludeCheckDigit) | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation_Uni |
USPS Planet | IDAutomation_Uni_Planet (DataToEncode, IncludeCheckDigit) | IDAutomation_UniversalFont_VBA.bas or IDautomation_WindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation_Uni |
Functions for the DataBar Barcode Font | |||
The functions listed below are preceded with IDAutomation_GS1 and are only provided with the IDAutomation DataBar Font Advantage Package. | |||
Barcode Type | DataBar Barcode Function Methods | Notes | VBA File to Import | Required Font |
DataBar | DataBar (DataToEncode) | IDAutomationGS1DataBar.bas | IDAutomation DataBar 34 |
DataBar Expanded | DataBarExpanded (DataToEncode, 22) | IDAutomationGS1DataBar.bas | IDAutomation DataBar 34 |
DataBar Exp. Stacked | DataBarExpanded (DataToEncode, Segments) | IDAutomationGS1DataBar.bas | IDAutomation DataBar 34 |
DataBar Stacked Omnidirectional | DataBarStacked (DataToEncode) | IDAutomationGS1DataBar.bas | IDAutomation DataBar 34 |
The functions listed below are rare and not approved for POS systems. | |||
DataBar Limited | DataBarLimited (DataToEncode) (Not approved for POS) | IDAutomationNativeWindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation DataBar 13 |
DataBar Stacked | DataBarStacked (DataToEncode) (Not approved for POS) | IDAutomationNativeWindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation DataBar 13 |
DataBar Truncated | DataBar (DataToEncode) (Not approved for POS) | IDAutomationNativeWindowsDLLVBA.bas and IDAutomationNativeFontEncoder.dll | IDAutomation DataBar 13 |
2D Font Functions | |||
Regarding all symbologies listed in this section below:
| |||
Barcode Type | 2D Function Methods | Notes | VBA File to Import | Required Font |
Data Matrix | IDAutomation_DMatrix_FontEncoder(DataToEncode As String, Optional ProcessTilde, Optional EncodingMode, Optional PreferredFormat) Complete VBA is only provided in the purchased version. | IDAutomation_DM_FontEncoder.bas (2020 Release) | IDAutomation2D or IDAutomationDMatrix |
EncDM(DataToEncode As String, Optional ProcessTilde, Optional EncodingMode, Optional PreferredFormat) | IDAutomation_Datamatrix_ActiveX_Macro.bas and IDAutomationDMatrix.dll (Legacy ActiveX Version) | ||
QR-Code | IDAutomation_QRFontEncoder(DataToEncode) All parameters are set to default except ApplyTilde which is set to 1. | IDAutomation_VBA_QRCode_FE2021 (2021 Release) | IDAutomation2D |
PDF417 | IDAutomation_PDF417(DataToEncode As String, Optional EccLevel As Integer, Optional ColumnSpecify As Integer, Optional RowSpecify As Integer, Optional Truncate As Integer, Optional ForceBinary As Integer) As String | IDAutomation_Native_PDF417_Macro.bas | IDAutomation2D or IDAutomationPDF417 |
IDAutomation_PDF417(DataToEncode As String, Optional EcLevel As Integer, Optional TotalColumns As Integer, Optional TotalRows As Integer, Optional Truncated As Integer, Optional PDFMode As Integer, Optional ApplyTilde As Integer) As String | IDAutomation_PDF417_Macro.bas and IDAutomationPDF417.dll (Legacy ActiveX Version) | ||
Aztec | EncAztec(DataToEncode As String, Optional ProcessTilde, Optional Error Correction) | IDAutomation_Aztec_Macro.bas and IDAutomationAztec.dll | IDAutomation2D |
Maxicode | EncMC(DataToEncode as String, Optional EncodingMode as Int) The default Encoding Mode is 2 | IDAutomation_MaxiCode_Macros.bas | IDAutomation Maxicode |
Barcode Function Descriptions
- ApplyTilde: If set to '1' or 'True', characters following the tilde may be used to perform additional calculations or encode ASCII characters directly.
- DataToEncode: This string value represents the data being encoded.
- N_Dimension: Determines the width of the wide bars when applicable, which is a multiple of the X dimension. Valid values are 2, 2.5 and 3. The default is 2. The X dimension is determined by the font point size.
- IncludeCheckDigit: A Boolean value that determines whether a check digit should be automatically calculated and included for the DataToEncode.
- Segments: Reducing segments to a low, even number such as 4, 6 or 8 in a DataBar Expanded symbol can create a stacked barcode. The default is 22.
** The USPS Intelligent Mail (IMb) function is supplied in pure VBA code with the purchased version of the associated font package.
This is a tutorial about writing code in Excel spreadsheets using Visual Basic for Applications (VBA).
Excel is one of Microsoft’s most popular products. In 2016, the CEO of Microsoft said 'Think about a world without Excel. That's just impossible for me.” Well, maybe the world can’t think without Excel.
- In 1996, there were over 30 million users of Microsoft Excel (source).
- Today, there are an estimated 750 million users of Microsoft Excel. That’s a little more than the population of Europe and 25x more users than there were in 1996.
We’re one big happy family!
In this tutorial, you’ll learn about VBA and how to write code in an Excel spreadsheet using Visual Basic.
Prerequisites
You don’t need any prior programming experience to understand this tutorial. However, you will need:
- Basic to intermediate familiarity with Microsoft Excel
- If you want to follow along with the VBA examples in this article, you will need access to Microsoft Excel, preferably the latest version (2019) but Excel 2016 and Excel 2013 will work just fine.
- A willingness to try new things
Learning Objectives
Over the course of this article, you will learn:
- What VBA is
- Why you would use VBA
- How to get set up in Excel to write VBA
- How to solve some real-world problems with VBA
Important Concepts
Here are some important concepts that you should be familiar with to fully understand this tutorial.
Objects: Excel is object-oriented, which means everything is an object - the Excel window, the workbook, a sheet, a chart, a cell. VBA allows users to manipulate and perform actions with objects in Excel.
If you don’t have any experience with object-oriented programming and this is a brand new concept, take a second to let that sink in!
Procedures: a procedure is a chunk of VBA code, written in the Visual Basic Editor, that accomplishes a task. Sometimes, this is also referred to as a macro (more on macros below). There are two types of procedures:
- Subroutines: a group of VBA statements that performs one or more actions
- Functions: a group of VBA statements that performs one or more actions and returns one or more values
Note: you can have functions operating inside of subroutines. You’ll see later.
Macros: If you’ve spent any time learning more advanced Excel functionality, you’ve probably encountered the concept of a “macro.” Excel users can record macros, consisting of user commands/keystrokes/clicks, and play them back at lightning speed to accomplish repetitive tasks. Recorded macros generate VBA code, which you can then examine. It’s actually quite fun to record a simple macro and then look at the VBA code.
Please keep in mind that sometimes it may be easier and faster to record a macro rather than hand-code a VBA procedure.
For example, maybe you work in project management. Once a week, you have to turn a raw exported report from your project management system into a beautifully formatted, clean report for leadership. You need to format the names of the over-budget projects in bold red text. You could record the formatting changes as a macro and run that whenever you need to make the change.
Visual Basic for Applications is a programming language developed by Microsoft. Each software program in the Microsoft Office suite is bundled with the VBA language at no extra cost. VBA allows Microsoft Office users to create small programs that operate within Microsoft Office software programs.
Think of VBA like a pizza oven within a restaurant. Excel is the restaurant. The kitchen comes with standard commercial appliances, like large refrigerators, stoves, and regular ole’ ovens - those are all of Excel’s standard features.
But what if you want to make wood-fired pizza? Can’t do that in a standard commercial baking oven. VBA is the pizza oven.
Yum.
Because wood-fired pizza is the best!
But seriously.
A lot of people spend a lot of time in Excel as a part of their jobs. Time in Excel moves differently, too. Depending on the circumstances, 10 minutes in Excel can feel like eternity if you’re not able to do what you need, or 10 hours can go by very quickly if everything is going great. Which is when you should ask yourself, why on earth am I spending 10 hours in Excel?
Sometimes, those days are inevitable. But if you’re spending 8-10 hours everyday in Excel doing repetitive tasks, repeating a lot of the same processes, trying to clean up after other users of the file, or even updating other files after changes are made to the Excel file, a VBA procedure just might be the solution for you.
You should consider using VBA if you need to:
- Automate repetitive tasks
- Create easy ways for users to interact with your spreadsheets
- Manipulate large amounts of data
Developer Tab
To write VBA, you’ll need to add the Developer tab to the ribbon, so you’ll see the ribbon like this.
To add the Developer tab to the ribbon:
- On the File tab, go to Options > Customize Ribbon.
- Under Customize the Ribbon and under Main Tabs, select the Developer check box.
After you show the tab, the Developer tab stays visible, unless you clear the check box or have to reinstall Excel. For more information, see Microsoft help documentation.
VBA Editor
Navigate to the Developer Tab, and click the Visual Basic button. A new window will pop up - this is the Visual Basic Editor. For the purposes of this tutorial, you just need to be familiar with the Project Explorer pane and the Property Properties pane.
First, let’s create a file for us to play around in.
- Open a new Excel file
- Save it as a macro-enabled workbook (. xlsm)
- Select the Developer tab
- Open the VBA Editor
Let’s rock and roll with some easy examples to get you writing code in a spreadsheet using Visual Basic.
Example #1: Display a Message when Users Open the Excel Workbook
In the VBA Editor, select Insert -> New Module
Write this code in the Module window (don’t paste!):
Sub Auto_Open()
MsgBox ('Welcome to the XYZ Workbook.')
End Sub
Save, close the workbook, and reopen the workbook. This dialog should display.
Ta da!
How is it doing that?
Depending on your familiarity with programming, you may have some guesses. It’s not particularly complex, but there’s quite a lot going on:
- Sub (short for “Subroutine): remember from the beginning, “a group of VBA statements that performs one or more actions.”
- Auto_Open: this is the specific subroutine. It automatically runs your code when the Excel file opens - this is the event that triggers the procedure. Auto_Open will only run when the workbook is opened manually; it will not run if the workbook is opened via code from another workbook (Workbook_Open will do that, learn more about the difference between the two).
- By default, a subroutine’s access is public. This means any other module can use this subroutine. All examples in this tutorial will be public subroutines. If needed, you can declare subroutines as private. This may be needed in some situations. Learn more about subroutine access modifiers.
- msgBox: this is a function - a group of VBA statements that performs one or more actions and returns a value. The returned value is the message “Welcome to the XYZ Workbook.”
In short, this is a simple subroutine that contains a function.
When could I use this?
Maybe you have a very important file that is accessed infrequently (say, once a quarter), but automatically updated daily by another VBA procedure. When it is accessed, it’s by many people in multiple departments, all across the company.
- Problem: Most of the time when users access the file, they are confused about the purpose of this file (why it exists), how it is updated so often, who maintains it, and how they should interact with it. New hires always have tons of questions, and you have to field these questions over and over and over again.
- Solution: create a user message that contains a concise answer to each of these frequently answered questions.
Real World Examples
- Use the MsgBox function to display a message when there is any event: user closes an Excel workbook, user prints, a new sheet is added to the workbook, etc.
- Use the MsgBox function to display a message when a user needs to fulfill a condition before closing an Excel workbook
- Use the InputBox function to get information from the user
Example #2: Allow User to Execute another Procedure
In the VBA Editor, select Insert -> New Module
Write this code in the Module window (don’t paste!):
Sub UserReportQuery()
Dim UserInput As Long
Dim Answer As Integer
UserInput = vbYesNo
Answer = MsgBox('Process the XYZ Report?', UserInput)
If Answer = vbYes Then ProcessReport
End Sub
Sub ProcessReport()
MsgBox ('Thanks for processing the XYZ Report.')
End Sub
Save and navigate back to the Developer tab of Excel and select the “Button” option. Click on a cell and assign the UserReportQuery macro to the button.
Now click the button. This message should display:
Click “yes” or hit Enter.
Once again, tada!
Please note that the secondary subroutine, ProcessReport, could be anything. I’ll demonstrate more possibilities in example #3. But first...
How is it doing that?
This example builds on the previous example and has quite a few new elements. Let’s go over the new stuff:
- Dim UserInput As Long: Dim is short for “dimension” and allows you to declare variable names. In this case, UserInput is the variable name and Long is the data type. In plain English, this line means “Here’s a variable called “UserInput”, and it’s a Long variable type.”
- Dim Answer As Integer: declares another variable called “Answer,” with a data type of Integer. Learn more about data types here.
- UserInput = vbYesNo: assigns a value to the variable. In this case, vbYesNo, which displays Yes and No buttons. There are many button types, learn more here.
- Answer = MsgBox(“Process the XYZ Report?”, UserInput): assigns the value of the variable Answer to be a MsgBox function and the UserInput variable. Yes, a variable within a variable.
- If Answer = vbYes Then ProcessReport: this is an “If statement,” a conditional statement, which allows us to say if x is true, then do y. In this case, if the user has selected “Yes,” then execute the ProcessReport subroutine.
When could I use this?
This could be used in many, many ways. The value and versatility of this functionality is more so defined by what the secondary subroutine does.
For example, maybe you have a file that is used to generate 3 different weekly reports. These reports are formatted in dramatically different ways.
- Problem: Each time one of these reports needs to be generated, a user opens the file and changes formatting and charts; so on and so forth. This file is being edited extensively at least 3 times per week, and it takes at least 30 minutes each time it’s edited.
- Solution: create 1 button per report type, which automatically reformats the necessary components of the reports and generates the necessary charts.
Real World Examples
- Create a dialog box for user to automatically populate certain information across multiple sheets
- Use the InputBox function to get information from the user, which is then populated across multiple sheets
Example #3: Add Numbers to a Range with a For-Next Loop
For loops are very useful if you need to perform repetitive tasks on a specific range of values - arrays or cell ranges. In plain English, a loop says “for each x, do y.”
In the VBA Editor, select Insert -> New Module
Write this code in the Module window (don’t paste!):
Sub LoopExample()
Dim X As Integer
For X = 1 To 100
Range('A' & X).Value = X
Next X
End Sub
Microsoft Excel Tutorials Free
Save and navigate back to the Developer tab of Excel and select the Macros button. Run the LoopExample macro.
This should happen:
Etc, until the 100th row.
How is it doing that?
- Dim X As Integer: declares the variable X as a data type of Integer.
- For X = 1 To 100: this is the start of the For loop. Simply put, it tells the loop to keep repeating until X = 100. X is the counter. The loop will keep executing until X = 100, execute one last time, and then stop.
- Range('A' & X).Value = X: this declares the range of the loop and what to put in that range. Since X = 1 initially, the first cell will be A1, at which point the loop will put X into that cell.
- Next X: this tells the loop to run again
When could I use this?
The For-Next loop is one of the most powerful functionalities of VBA; there are numerous potential use cases. This is a more complex example that would require multiple layers of logic, but it communicates the world of possibilities in For-Next loops.
Maybe you have a list of all products sold at your bakery in Column A, the type of product in Column B (cakes, donuts, or muffins), the cost of ingredients in Column C, and the market average cost of each product type in another sheet.
Excel 2008 Tutorial For Mac Free Download
You need to figure out what should be the retail price of each product. You’re thinking it should be the cost of ingredients plus 20%, but also 1.2% under market average if possible. A For-Next loop would allow you to do this type of calculation.
Real World Examples
- Use a loop with a nested if statement to add specific values to a separate array only if they meet certain conditions
- Perform mathematical calculations on each value in a range, e.g. calculate additional charges and add them to the value
- Loop through each character in a string and extract all numbers
- Randomly select a number of values from an array
Excel For Mac Tutorial Free
Now that we’ve talked about pizza and muffins and oh-yeah, how to write VBA code in Excel spreadsheets, let’s do a learning check. See if you can answer these questions.
- What is VBA?
- How do I get set up to start using VBA in Excel?
- Why and when would you use VBA?
- What are some problems I could solve with VBA?
If you have a fair idea of how to you could answer these questions, then this was successful.
Whether you’re an occasional user or a power user, I hope this tutorial provided useful information about what can be accomplished with just a bit of code in your Excel spreadsheets.
Happy coding!
Learning Resources
- Excel VBA Programming for Dummies, John Walkenbach
A bit about me
Microsoft Excel For Mac Tutorial
I'm Chloe Tucker, an artist and developer in Portland, Oregon. As a former educator, I'm continuously searching for the intersection of learning and teaching, or technology and art. Reach out to me on Twitter @_chloetucker and check out my website at chloe.dev.