0% found this document useful (0 votes)
70 views

VBA VBA VBA Vba Summary: 1. VBA Sub and Function Procedures

VBA allows you to write macros to automate tasks in Excel. It has various elements like sub and function procedures, variables, arrays, and worksheet functions. Sub procedures contain VBA code that runs but does not return a value, while function procedures return a value. Ranges represent cells or groups of cells and have properties like value, font, and formula that can be accessed. Loops like If-Then allow code to be conditionally executed. Worksheet functions like SUM and VLOOKUP can be used in VBA code. Comments, variables, arrays, and other elements allow for complex macro development to automate tasks in Excel.

Uploaded by

James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

VBA VBA VBA Vba Summary: 1. VBA Sub and Function Procedures

VBA allows you to write macros to automate tasks in Excel. It has various elements like sub and function procedures, variables, arrays, and worksheet functions. Sub procedures contain VBA code that runs but does not return a value, while function procedures return a value. Ranges represent cells or groups of cells and have properties like value, font, and formula that can be accessed. Loops like If-Then allow code to be conditionally executed. Worksheet functions like SUM and VLOOKUP can be used in VBA code. Comments, variables, arrays, and other elements allow for complex macro development to automate tasks in Excel.

Uploaded by

James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

VBA SUMMARY

1. VBA Sub and Function Procedures


Sub Procedure
- Display purposes
Sub SubProcedureName ()
Statements
End Sub

Function Procedure
- Returns a value
Function FunctionName (parameters)
Variable = Value
End Function
- Executing a function
From a Sub procedure
Sub CallerSub ()
Answer = FunctionName (ParameterValue)
MsgBox Answer
End Function
Use the function in a worksheet formula
= FunctionName (ParameterValue)

2. Essential VBA Language Elements


Comments

Some comments

Declare variable
- Dim - Dim VariableName Type

Dim MyName As String

- Public - variable available to all the procedures


- Static - retain their value even when the procedure ends
- Private - Const - constants

Operators
Add

Integer division

Multiply

Modulus

Mod

Divide

Logical Not

Not

Subtracts

Logical And

And

Power

Logical Or

Or

Concatenate

&

Logical XOR

XoR

Equivalent

Eqv

Implicit

Imp

Arrays
-

declare an array

Dim MyArray (1 To 100) As Double

Multidimensional array

Dim MyArray (1 To 3, 1 To 3 ) As Double

Assign value to array index

MyArray (i, j) = Value

Dynamic arrays have no pre-set number of elements

Dim MyArray () As Double

Change the number of elements in a dynamic array

ReDim MyArray (1 to 20)

Avoid destroying the old values

ReDim Preserve MyArray (1 to 20)

3. Range Objects
Range object
- Represents a range contained in a Worksheet object
Worksheets (SheetName).Range (A1:C5)

Value property
Assign value to cell
Worksheets (SheetName).Range (A1).Value = Value

Text property
returns a string that represents the text cells - the formatted value
Range (A1:C3).Count

Count property
returns the number of cells in a range

Font property
returns a Font object
Range (A1).Font.Bold = True

Interior property
returns an interior object
Range (A1).Interior.Color = 00000000

Formula property
represents the formula in a cell
Range (A1).Formula = =SUM (A1:A7)

NumberFormat property
represents the formula in a cell
Columns(A:A).NumberFormat = 0.00

Select method
selects a range of cells
Worksheets(SheetName).Activate
Range(A1:A2).Select

Goto method
Go to selected range/sheet
Application.Goto Reference:=Worksheets("Sheet2").Range("A1")

Copy and Paste methods


Range(A1:A12).Copy Range(C1

Clear/ClearContents/ClearFormats method
Deletes the contents of a range and cell formatting
Columns (D:D).Clear
ClearContents deletes the contents of the range but leaves the formatting intact
ClearFormats - deletes the formatting in the range but not the cell contents

4. VBA and Worksheet Functions


Total = Application.WorksheetFunction.Sum(Range(A1:A12))

VLOOKUP function
Price = WorksheetFunction.VLOOKUP(PartNum, Range(PriceList), 2,
False)

5. Loops
The If-Then structure
Sub SubProcedureName()
If Condition Then
Statements
Else
Statements
End If
End Sub

You might also like