Microsoft Excel includes a comprehensive macro programming language called VBA. This programming language provides you with at least three additional resources:
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
You can find a good explanation on why other methods are discouraged/inaccurate here : http://stackoverflow.com/a/11169920/4628637
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 :
We all know them, but these practices are far less obvious to someone starting to program in VBA.
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.
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.
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.
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).
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.
'To use Autofilter within VBA we need to call with at least the following parameters:
Sheet("MySheet").Range("MyRange").Autofilter Field=(ColumnNumberWithin"MyRange"ToBeFilteredInNumericValue) Criteria1:= "WhatIWantToFilter"
'There are plenty of examples either on the web or here at stackoverflow
*) 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.
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.