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

AIN3701 VBA CodingSheet 2024 1

notes

Uploaded by

Given Moshakga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

AIN3701 VBA CodingSheet 2024 1

notes

Uploaded by

Given Moshakga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

VBA CODING SHEET

2024

Prepared by:
AIN3701 Lecturers
AIN3701 Coding Sheet - 2024

Dear Students

Welcome to the VBA Coding Sheet, a streamlined guide designed specifically for third-year
accounting students. This resource is crafted to support your learning journey in mastering
Visual Basic for Applications (VBA), a powerful tool used extensively in the accounting and
finance industries for automating tasks and enhancing productivity.

It is important to note that this coding sheet is not a replacement for your primary study
materials, or the invaluable experience gained from hands-on coding practice. Instead, it serves
as an additional aid, offering quick references to key concepts, syntax, and common functions.
Use it to reinforce your understanding, quickly recall essential information, and navigate
through your VBA assignments more efficiently.

Remember, true proficiency in VBA comes from a combination of thorough study and
consistent practice. Use this coding sheet to complement your learning process and to help
bridge the gap between theoretical knowledge and practical application.

Happy coding!

Kind regards

AIN3701 Lecturers

2
AIN3701 Coding Sheet - 2024

Basic Programming Operations


Operation Code

Declare a variable Dim myVar as Datatype

Set the value of a variable myVar = “some value”

Set the value of an object variable Set myObj = Worksheets(“myWorksheet”)

Declare a constant Const myCon as Datatype = 0.15


userInput = InputBox(“What is your favourite
Gather user input
subject?”)
Print a message to the screen MsgBox(“Hello World !!!”)

Comment code ‘This is my comment

Basic Operations on Data


Operation Code

Addition imTwo = 1 + 1

Subtraction imZero = 1 – 1

Multiplication imTen = 2 * 5

Division (uses “/” operator) MsgBox(10/3) ‘returns 3.333333

Integer division (uses “\” operator) MsgBox(10/3) ‘returns 3

Concatenation helloWorld = “hello” & “World”

3
AIN3701 Coding Sheet - 2024

Data Types
Data Type Description Bytes Example

Integer Whole number between -32 768 and 32 767 2 11


Whole numbers between – 2 147 483 648 and
Long 4 1 234 567
2 147 483 647
Single Decimal number with seven digits of precision 4 3.141593

Double Decimal number with fifteen digits of precision 8 3.14159265358979

Date Date values 8 15/10/2024


10 +
String Text data string “Hello World”
length
Boolean Logical true or false values 2 True
Set mySheet =
Worksheet Worksheet object in Excel 4
Sheets(“Sheet1”)
Set myWorkbook =
Workbook Workbook object in Excel 4
Workbooks(1)
myVariant =
Variant Unspecified data type 16+
“Anything goes!”

4
AIN3701 Coding Sheet - 2024

Logical / Comparison Operators


Operator Symbol Code

Equals = 5=5

Not equals <> 5 <> 6

Greater than > 2>1

Greater than or equal to >= 2>=2

Less than < 4<5

Less than or equal to <= 4<=5


(5 = 5) And (5 <> 55) = True
And And (5 = 5) And (5 = 55) = False
(5 <> 5) And (5 = 55) = False
(5 = 5) Or (5 <> 55) = True
Or Or (5 = 5) Or (5 = 55) = True
(5 <> 5) Or (5 = 55) = False
Not (5 = 5) = False
Not Not
Not (5 = 55) = True

5
AIN3701 Coding Sheet - 2024

If Statements
Type Example scenario VBA Code
If the value stored in the variable
If val > 1000 then
“val” is greater than 1 000, print the
Simple If statement Msgbox(“Large)
text “Large”.
End If
Otherwise do nothing.
If val > 1000 then
If the value stored in the variable
Msgbox(“Large”)
“val” is greater than 1 000, print the
If-Else statement Else
text “Large”.
Msgbox(“Small”)
Otherwise, print the text “Small”.
End If
If the value stored in the variable If val > 1000 then
“val” is greater than 1 000, print the Msgbox(“Large”)
text “Large”. ElseIf val >= 200 Then
If-Elseif-Else statement If the value stored in the variable Msgbox(“Medium”)
“val” is between 200 and 1000, print Else
the text “Medium”. Msgbox(“Small”)
Otherwise, print the text “Small”. End If

Select Case Statement


Type Example scenario VBA Code
Select Case var
If the value stored in the variable
Case Is > 1000
“val” is greater than 1 000, print the
Msgbox(“Large”)
text “Large”.
Case Is >= 200
Select Case statement If the value stored in the variable
Msgbox(“Medium”)
“val” is between 200 and 1000, print
Case Else
the text “Medium”.
Msgbox(“Small”)
Otherwise, print the text “Small”.
End Select

6
AIN3701 Coding Sheet - 2024

Loops
Type Example scenario VBA Code
Dim Counter as Integer

For Next Loop Print the first 5 integers to the screen For Counter = 1 to 5
Msgbox(Counter)
Next Counter
Dim Counter as Integer

Counter = 1
Do While Loop Print the first 5 integers to the screen Do While Counter <= 5
Msgbox(Counter)
Counter = Counter + 1
Loop
Dim Counter as Integer

Counter = 1
Do Until Loop Print the first 5 integers to the screen Do Until Counter > 5
Msgbox(Counter)
Counter = Counter + 1
Loop

7
AIN3701 Coding Sheet - 2024

Working with Ranges


Example Scenario VBA Code

Target a single cell using a hard-coded reference Range(“A1”)

Target multiple cells using a hard-coded reference Range(“A1:C3”)

Print the value of a cell using a hard-coded reference MsgBox(Range(“A1”).Value)

Set the value of a cell using a hard-coded reference Range(“A1”).Value = 11

Print the value of the active cell MsgBox(ActiveCell.Value)

Set the value of the active cell ActiveCell.Value = 22


Print the value of the cell 1 row below, and 2 columns to
MsgBox(ActiveCell.Offset(1,2))
the right, of the active cell
Set the value of the cell 1 row above,and 2 columns to ActiveCell.Offset(-1,-2).Value = “I’m
the left, of the active cell upset that I’ve been offset!”
Use the Cells property to print the value of cell A1 MsgBox(Cells(1,1))

Use the Cells property to set the value of cell D3 Cells(3,4).Value = “Row 3, column 4”
Dim LastRow as Single
Select the last row where data is entered Lastrow = Cells(Rows.Count,
1).End(xlUp).Row

8
AIN3701 Coding Sheet - 2024

Working with Worksheets


Example Scenario VBA Code

Activate a sheet by referencing its name Sheets(“Sheet 1”).Activate

Activate a sheet by referencing its index Sheets(1).Activate

Print the name of the active worksheet MsgBox(Activesheet.Name)

Add a new worksheet Sheets.Add

Add a new worksheet and specify its name Sheets.Add.Name = “My New Sheet”

Delete a worksheet Sheets(“My New Sheet”).Delete

Hide a worksheet Sheets(2).visible = False

Unhide a worksheet Sheets(2).visible = True

Working with Workbooks (Windows OS)


Example Scenario VBA Code

Activate a workbook by referencing its name Workbooks(“My Workbook”).Activate

Print the name of the active workbook MsgBox(ActiveWorkbook.Name)

Add a new workbook Workbooks.Add

Open a workbook Workbooks.Open(“C:\My Workbook.xlsx”)

Save a workbook Workbooks(“My Workbook”).Save


Workbooks(“My Workbook”).Close
Close a workbook and save changes
SaveChanges:=True
Workbooks(“My Workbook”).Close
Close a workbook without saving changes
SaveChanges:=False

9
AIN3701 Coding Sheet - 2024

More Advanced Programming Operations


Example Scenario VBA Code
Counter the effect where screen is updated while
Application.ScreenUpdating = False
code is running
Clear the moving border when cells are copied Application.CutCopyMode = False

10

You might also like