Return to site

Visual Basic Code Excel Examples

broken image


This is the snippet Automate Excel via VB - Example on FreeVBCode. The FreeVBCode site provides free Visual Basic code, examples, snippets, and articles on a variety of other topics as well. Add Serial Numbers. Sub AddSerialNumbers Dim i As Integer On Error GoTo Last i =. Now before I get into the Macro Example and give you the VBA code, let me first show you how to use these example codes. Using the Code from Excel Macro Examples. Here are the steps you need to follow to use the code from any of the examples: Open the Workbook in which you want to use the macro. Hold the ALT key and press F11. This opens the VB. One of the best ways to learn Excel VBA is by exploring code samples & examples. That is why, we publish detailed examples, downloadable workbooks & full-length tutorials from time to time. In this page, you can find several Excel VBA examples. Click on any link to see the full example & get downloadable workbook.

How to update an Excel spreadsheet using ADO An example can be found at the bottom of the post. Option Explicit 'Purpose: Updates the contents of an Excel Spreadsheet using ADO 'Inputs: sWorkbookPath The path of the workbook to update the range contents of. ' sRange The range name or range reference to update (eg.

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.

Microsoft Excel Visual Basic Examples

Visual Basic Editor view:

10 Useful Examples of Macros for Accounting:

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

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:

Examples

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

Visual Basic Code Excel Examples

Example code:

Microsoft Visual Basic For Excel

Excel visual basic programming examples

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:

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.

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:

Microsoft Visual Basic For Excel

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:

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:

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 could create, the sky is the limit. You can develop educational apps, financial apps, games, multimedia apps, animations, database applications and more.

The team at vbtutor.net has created many sample codes, please browse them on the sidebar. You are welcome to use the sample codes as a reference for your assignments and projects. However, the usage of the sample codes for commercial purposes without prior consent from the webmaster is strictly prohibited.


Visual Basic Sample Codes E-Book is written by our webmaster, Dr.Liew. It comprises 258 pages of captivating contents and 48 fascinating Sample Codes.Perfect source of reference for your VB projects. Check it out.

About Us

The Tutor and webmaster of Vbtutor.net, Dr.Liew Voon Kiong , holds a Bachelor's Degree in Mathematics, a Master's Degree in Management and a Doctoral Degree in Business Administration. He obtained the DBA degree from the University of South Australia.

He has been involved in programming for more than years. He created the popular online Visual Basic Tutorial in 1996 and since then the web site has attracted millions of visitors .It is the top-ranked Visual Basic tutorial website in many search engines including Google. Besides that, he has also written a few Visual Basic related books. One of the books, Visual Basic 6 Made Easy was published by Creativespace.com, an Amazon.com publisher.


Excel Vba Macro Examples

The Slot Machine created using VB6



Excel Visual Basic Programming Examples

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy





broken image