VBA Data Types
Variant |
any data type |
Integer |
(2 bytes) integer |
Long |
(4 bytes) integer |
Single |
(4 bytes) floating point |
Double |
(8 bytes) floating point |
String |
non-numeric data (declared with quotes) |
Object |
any object reference |
Date |
a date |
Boolean |
True / False |
Byte |
0-255 |
Operators (Syntax)
Comparison |
=, <> (not equal to), >, < , >= , <= |
Logical (Boolean) |
NOT (opposite), AND (if all true then returns true), OR (at least 1 true returns true) |
Mathematical |
+, -, *, / , \ (integer division), Mod (remainder), ^ (remember to put space) |
String Concatenation |
& |
The concatenate operator eg. "A" & "B" becomes "AB".
Operators (Precedence)
1 |
^ |
2 |
* OR / (division) |
3 |
\ (integer division) |
4 |
Mod |
5 |
+ OR - |
Declarations
Variables |
|
Arrays |
Dim [arrayname(index)] As [type]
|
Re-declare Array |
ReDim [arrayname(newindex?)] As [newtype?]
|
ReDim and keep values |
ReDim Preserve [arrayname(newindex?)] As [newtype?]
|
Option Explicit
is used to require declarations of all variables. Option Base 1
makes the index of all arrays starts from 1. If declared array has no numeral index
provided, then it is dynamic in size.
|
|
Data Functions (Conversions)
...value to a boolean |
|
...value to a integer |
|
...value to a double |
|
...value to a string |
|
Val
function accepts a string as input and returns the numbers found in that string.
Math Functions
Absolute |
|
Square root |
|
Exponential, e |
|
Natural log, ln |
|
Is it a number (boolean)? |
IsNumeric([numeric value])
|
Truncate to integer |
|
|
|
Round to a decimal place |
Round([numeric value],[# of digits])
|
String Functions
All upper case |
|
All lower case |
|
Length of string (integer) |
|
Filters a string to a double |
|
Convert number to string |
|
Val
function accepts a string as input and returns the numbers found in that string.
User Interaction and Cell Selection
A popup dialogue box |
MsgBox "dialogue" [& variable etc.]
|
Prompt user for input |
|
...a single cell A1 |
|
...an active cell |
|
...a contiguous range |
Range("A1:G5").Select
|
...offset and select |
[ActiveCell/Range(#)].Offset(1,0).Select
|
...set a cell's value |
[ActiveCell/Range(#)].Value = [varname]
|
Use Range
to select specific cells or group of cells. Use ActiveCell
to select highlighted cell in excel.
Array Functions
Highest element number |
UBound(ArrayName [, Dimension] )
|
Lowest element number |
LBound(ArrayName [, Dimension] )
|
Highest element number is the size of possible entries a array can hold.
Array Iteration
Function MinIntegerofArray(TheArray As Variant) As Integer
Dim i As Integer, placeholder As Integer
placeholder = 0
For i = 1 To UBound(TheArray)
If TheArray(i) < TheArray(placeholder) Then
placeholder = i
End If
Next
'index of min value is at placeholder
MinIntegerofArray = TheArray(placeholder)
End Function
|
Error Handling with GoTo & Labels
...
tempstudentName = InputBox("Please enter student name (type exit to end): ")
'error check and force reentry of input
If IsText(tempstudentName) = False Then
checker1:
tempstudentName = InputBox("Please enter a valid student name [not blank and letter] (type exit to end): ")
End If
If tempstudentName = "exit" Then
End '(the program)
ElseIf IsText(tempstudentName) = False Then
GoTo checker1 'label
Else
studentName = tempstudentName
End If
...
|
'checker1:' is a label: labels only include the next line
IsText, Case Statements, For loop
Function IsText(streng As Variant) As Boolean
Dim i As Integer
For i = 1 To Len(streng)
'checks if the text follows the ACII numerials (a-z AND A-Z)
Select Case Asc(Mid(streng, i, 1))
Case 65 To 90, 97 To 122
IsText = True
Case Else
IsText = False
Exit For
End Select
Next
End Function
|
|
Created By
Metadata
Favourited By
Comments
jeffunsal, 02:31 26 Jul 22
Very useful chearsheet for vba. Thank you very much.
Add a Comment
Related Cheat Sheets