Escolar Documentos
Profissional Documentos
Cultura Documentos
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)
Some comments
Declare variable
- Dim - Dim VariableName Type
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
Multidimensional array
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")
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
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