excel-vba

Topics related to excel-vba:

Getting started with excel-vba

Microsoft Excel includes a comprehensive macro programming language called VBA. This programming language provides you with at least three additional resources:

  1. Automatically drive Excel from code using Macros. For the most part, anything that the user can do by manipulating Excel from the user interface can be done by writing code in Excel VBA.
  2. Create new, custom worksheet functions.
  3. Interact Excel with other applications such as Microsoft Word, PowerPoint, Internet Explorer, Notepad, etc.

VBA stands for Visual Basic for Applications. It is a custom version of the venerable Visual Basic programming language that has powered Microsoft Excel's macros since the mid-1990s.

IMPORTANT
Please ensure any examples or topics created within the excel-vba tag are specific and relevant to the use of VBA with Microsoft Excel. Any suggested topics or examples provided that are generic to the VBA language should be declined in order to prevent duplication of efforts.

  • on-topic examples:

    Creating and interacting with worksheet objects
    The WorksheetFunction class and respective methods
    Using the xlDirection enumeration to navigate a range

  • off-topic examples:

    How to create a 'for each' loop
    MsgBox class and how to display a message
    Using WinAPI in VBA


Debugging and Troubleshooting

Methods for Finding the Last Used Row or Column in a Worksheet

User Defined Functions (UDFs)

A User Defined Function (aka UDF) refers to a task specific function that has been created by the user. It can be called as a worksheet function (ex: =SUM(...)) or used to return a value to a running process in a Sub procedure. A UDF returns a value, typically from information passed into it as one or more parameters.

It can be created by :

  1. using VBA .
  2. using Excel C API - By creating an XLL that exports compiled functions to Excel.
  3. using the COM interface.

VBA Best Practices

We all know them, but these practices are far less obvious to someone starting to program in VBA.


Loop through all Sheets in Active Workbook

Ranges and Cells

Note that the variable names r, cell and others can be named however you like but should be named appropriately so the code is easier to understand for you and others.

Common Mistakes

Arrays

Excel VBA Tips and Tricks

This topic consists of a wide variety of useful tips and tricks discovered by SO users through their experience in coding. These are often examples of ways to circumvent common frustrations or ways of using Excel in a more "clever" way.

PowerPoint Integration Through VBA

This section demonstrates a variety of ways to interact with PowerPoint through VBA. From showing data on slides to creating charts, PowerPoint is a very powerful tool when used in conjunction with Excel. Thus, this section seeks to demonstrate the various ways VBA can be used to automate this interaction.

Workbooks

Pivot Tables

There are many excellent reference and example sources on the Web. Some examples and explanations are created here as a collection point for quick answers. More detailed illustrations may be linked to external content (instead of copying existing original material).

Binding

Charts and Charting

Application object

Excel VBA comes with a comprehensive object model which contains classes and objects that you can use to manipulate any part of the running Excel application. One of the most common objects you'll use is the Application object. This is a top-level catchall that represents the current running instance of Excel. Almost everything that is not connected to a particular Excel workbook is in the Application object.

The Application object, as a top-level object, has literally hundreds of properties, methods, and events which can be used to control every aspect of Excel.

Merged Cells / Ranges

VBA Security

How to record a Macro

Locating duplicate values in a range

Named Ranges

autofilter ; Uses and best practices

Creating a drop-down menu in the Active Worksheet with a Combo Box

Conditional statements

Working with Excel Tables in VBA

Excel-VBA Optimization

*) Line numbers represent are integers, that is a signed 16 bit data type in the range of -32,768 to 32,767, otherwise you produce an overflow. Usually line numbers are inserted in steps of 10 over a part of the code or all procedures of a module as a whole.

Conditional formatting using VBA

You cannot define more than three conditional formats for a range. Use the Modify method to modify an existing conditional format, or use the Delete method to delete an existing format before adding a new one.

File System Object

SQL in Excel VBA - Best Practices

Use Worksheet object and not Sheet object

CustomDocumentProperties in practice