Visual Basic Code Examples For Excel



VB.NET Excel examples

Open the Visual Basic Editor. In the Project Explorer, right-click the project name and select Import File. Navigate to the.bas file and click Open. Excel macro examples. One of the best ways to learn Excel VBA is by exploring code samples. Below you will find examples of very simple VBA codes that automate some basic operations. Visual Basic Sample Codes Visual Basic 6 is a third-generation event-driven programming language first released by Microsoft in 1991. In VB 6, there is no limit of what applications you.

On this page you will find a set of HowTo samples that can help you to get started with add-in development for Excel 2019, 2016, 2013, 2010 and lower:

And here are a few more VB.NET examples for other applications of the Microsoft Office 2019 - 2000 suite:

Excel extensions: COM add-in, RTD server

How to develop a COM add-in for Microsoft Office (Excel, Word and PowerPoint)

This sample shared COM add-in for Excel, Word and PowerPoint shows how you can use Add-in Express for Office and .net to add custom command bars and command bar controls, place new controls onto Office Ribbon tabs, create custom task panes, handle application-level events and more.
Download COM addin

How to customize Microsoft Excel ribbon and toolbar

See how to create custom ribbons and toolbars for Excel and how to integrate with an existing Excel tab.
Download add-in

How to customize Excel main menu, context menus and Backstage view

VB.NET sample shows how to create a custom context menu for Excel 2019, 2016, 2013, 2010, 2007 and lower; how to add your own Backstage view item and how to customize the main menu in Excel 2003 - 2000.
Download sample

How to build your first Excel RTD server in VB.NET

See how to build an RTD server step-by-step in the Developer's Guide. The RTD server project is written in Visual Basic .NET. See also a video showing how to create an RTD server.
Download RTD server

Working with Excel Object model

How to handle the WorkbookBeforeSave event

How to create a custom event when Excel calculation mode changes

This VB.NET Excel addin example provides a solution for missing event that should occur then the user changes the calculation mode.
Download addin

How to find the last used cell (row or column) in Excel

This Visual Basic sample explains some reliable methods for finding the last used cell in an Excel worksheet or a range.
Download Find Last Cell add-in

How to populate Excel workbooks and ranges with arrays

Example Of Vba Code

VB.NET code example shows how to use arrays to populate Excel with data; how to fill array with a query or range and insert into a worksheet, and more.
Download Populate with Arrays add-in

How to work with Excel tables & ranges using VB.NET

This sample Excel addin demonstrates how work with tables and ranges: create a new table or a range, insert a column or a row, sort, filter and more.
Download Excel Tables and Ranges add-in

Basic

How to work with Excel pivot tables

VB.NET code example shows how to automate pivot tables in Excel: create a PivotTable, add calculated fields, display or hide a field; delete, refresh or clear a pivot table; create a pivot chart and more.
Download Excel PivotTables Add-in

Advanced Excel Task Panes

How to show and hide Excel Task Panes programmatically

This Visual Basic .NET Excel sample demonstrates how to set the visibility of an advanced task pane to make it show up from the command bar.
Download VB.NET sample

How to switch between several task panes programmatically

This addin shows how you can use several custom task panes in one position and switch between them programmatically.
Download example

How to show an advanced Excel task pane dynamically

Examples

The Excel VB.NET add-in shows how to build a context-dependent custom task pane. The plug-in shows a task pane when cell A1 contains any value.
Download plug-in

How to re-size an Excel task pane in VB.NET

This Visual Basic .NET example shows how you can resize your custom form using Advanced Task Panes for Excel 2019 - 2000. To change the form size programmatically, you set the splitter to none. Otherwise, only the user can resize the task pane using the splitter.
Download sample plugin

User-defined functions

How to develop Excel Automation add-in

See how to create Excel Automation add-ins step-by-step in the Developer's Guide. The sample is written in VB.NET.
Download Automation add-in

How to develop XLL add-in

This sample project demonstrates how you can build an XLL addin in Visual Studio. You can find a detail description of this example in the Developer's Guide: How to create an XLL add-in.
Download XLL addin

Note. You can find plenty more HowTo examples on our technical blog. Be sure to check it out, we add new HowTo samples every week.

What are Macros?

They are a series of commands used to automate a repeated task. This can be run whenever the task must be performed.

How to access Macros

Click on the ‘View’ tab, at the end you’ll find the function ‘Macros’ arranged in the Macros group. Click the arrow under ‘Macros’ where you can manage your macro performances easily.

To edit a macro, click on the ‘Edit’ button, this will take you to the ‘Visual Basic Editor’ where you can easily modify the macro to do what you want.

Visual Basic Editor view:

10 Useful Examples of Macros for Accounting:

Excel Visual Basic Function Example

1. Macro: Save All

Helpful for saving all open excel workbooks at once. It is advised to run this macro before running other macros or performing tasks that’s could potentially freeze Microsoft Excel.

Name this macro ‘SaveAll’

Example code:

2. Macro: Comments and Highlights

Helpful is you use a lot of comments and highlights while editing worksheets. Running this macro generates a new tab at the front of the worksheet with a listing of every cell with a comment or highlight in the workbook. Each cell reference is a hyperlink that leads directly to the cell with the comment/highlight. The summary tab lists the value within the cell and the text in the comment.

Additionally, there is an ‘Accept’ button that removes the highlight or deletes the comment from the chosen cell. Once all changes are accepted, the summary tab will be deleted.

NB: This macro only sets up to find certain highlights of yellow, but this can be modified.

3. Macro: Insert a Check Mark

Visual Basic Code Samples For Excel

Helpful for footing (Adding a column of numbers) trial balances, schedules and reconciliations. It is often common to put a check mark below the total to show the column total is accurate.

This macro will insert a red check mark in the active cell. Once this is added all you will be required to do is select the cell you want to check-mark in and run the macro. Name this macro ‘Checkmark’.

Example code:

4. Macro: Mail Workbook

Helpful to send a lot of excel files via email. Running this macro creates a new Microsoft Outlook email with the last saved version of the open workbook. The email, by default, will not be addressed by anyone, the subject will be the name of the workbook, and the body will be ‘See attached’. However, these settings can be customized.

Example code:

5. Macro: Copying the Sum of Selected Cells

Helpful in copying the sum of several cell in a spreadsheet. These cells can be scattered throughout the spreadsheet or all in the same row/column.

Once this macro is added you need to highlight the cells you want to sum, then run the macro, and then paste into the cell you want the sum in.

NB: This will just paste the value of the sum, not a sum() formula

Example code:

6. Macro: Open Calculator

Helpful to open a calculator.

Example code:

Sub OpenCalculator()
Application.ActivateMicrosoftApp Index:=0
End Sub

7. Macro: Refresh All Pivot Tables

Helpful to refresh all pivot tables in the whole workbook in a single shot.

Example code:

Visual Basic Code Examples For Excel Download

8. Macro: Multiply all the Values by a Number

Helpful if you have a list of numbers you want to have multiplied by a particular number. Select the range of cells you need and run the example code below. It will first ask you for the number with whom you want to multiply and then instantly multiply all the numbers in the range with it.

Example code:

9. Macro: Add a Number to all the Numbers in Range

Similar to multiplying, you can also add a number to all the numbers in a particular range.

Example code:

10. Macro: Remove Negative Signs

Code that checks a selection and converts all the negative number into positive. Select a range and run the code.

Example code: