Dim StudentsWorksheet As Worksheet
Dim StudentsTable As Object
Set StudentsWorksheet = Sheets("Students")
Set StudentsTable = StudentsWorksheet.ListObjects("StudentsTable")
*Hint: It's a common convention to group all Dims before Sets.
Confirm that we retrieved the correct object:
Debug.Print(StudentsTable.Range.Rows.Count)
^^ See if you get the correct number of rows.
Range covers the entire range including headers.
But what if we don't want the headers?
Good resource obtained by Google searching "vba table body".
Dim StudentsBodyRange As Range
Set StudentsBodyRange = StudentsTable.DataBodyRange
*Hint: If you're targeting specific cells/columns/etc., make sure you have the referenced workbook in focus.
Make a random student selector
First, get the number of students by counting the rows.
Dim StudentsCount As Integer
StudentsCount = StudentsBodyRange.Rows.Count
Generate a random number between 1 and StudentsCount.
Rnd generates a random decimal number between 0 and 1.
Multiply Rnd by StudentsCount, and we get a random decimal number between 1 and 64.999*.
So we + 1 to allow it to go up to 65.
Use Int to round it to an integer.
Save this random number to a variable, RandomIndex.
Morning Exercise Review
TimBits
Using IFS to check multiple conditions
Good reference for
IFS
: https://exceljet.net/excel-functions/excel-ifs-functionGo through a series of conditions. If a condition is true, the value that follows the condition is returned.
Note that for all these examples, we use A1 cell as the reference.
IFS(MOD(A1, 15)=0,"TimBits", MOD(A1, 3)=0, "Tim", MOD(A1, 5)=0, "Bits")
If no conditions are met, it returns an NA error.
To avoid that, wrap the whole thing in an
IFERROR
and return a default value in case of errors.IFERROR(IFS(MOD(A1, 15)=0,"TimBits", MOD(A1, 3)=0, "Tim", MOD(A1, 5)=0, "Bits"), A1)
Another way of supplying default value: Add another set of condition & return value, where that condition evaluates to TRUE all the time.
IFS(MOD(A1, 15)=0,"TimBits", MOD(A1, 3)=0, "Tim", MOD(A1, 5)=0, "Bits", TRUE, A1)
Using nested IF statements
IF(MOD(A1, 15)=0, "TimBits", IF(MOD(A1, 3)=0, "Tim", IF(MOD(A1, 5)=0, "Bits")))
IFS
...VBA (Visual Basic for Applications)
Can be used to automate tasks across the Microsoft suite.
DEVELOPER Tab --> Visual Basic --> You'll see a Module (Module 1 by default)
Check out the first video above if you cannot find the DEVELOPER tab.
Option Explicit
Option Base 1
Sub
(sub procedure)Reference: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/sub-statement
Example:
TimBits
is the name we gave to this Sub.End Sub
.Execute the Macro
Declare a variable
For example, we can declare a variable
RowNum
by writing:Dim RowNum As Integer
As Integer
is needed because of our Option Explicit.Iterate with a for-loop
Example:
RowNum
's value starts from 1, and changes to 2, and to 3...and finally to 100.Print
Turn the code off (i.e. comment it out)
Add
'
before the lines of code you want to comment out. The line of code that's commented out will not run.Keywords in VBA
These are purple, and they are case-sensitive. e.g.
For
,Sub
,Next
,End Sub
,As
,Range
, etc.Reference: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/keywords-by-task
Conditional Logic (If, Else)
Example:
Note that the first ones take priority.
The
Else
is the catch-all (i.e. the default things to do).Styling & Formatting with VBA
See image below for reference.
Google is your best friend... Even experienced practitioners can't memorize all the properties and values.
StackOverflow.com is also an awesome place to search for answers, and it's great for all programming languages.
Data Types in VBA
The
Set
keyword: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/set-statementThis is a good, short video on how to use
Dim
andSet
.Exmple: Reference an object in a worksheet
*Hint: It's a common convention to group all
Dim
s beforeSet
s.Confirm that we retrieved the correct object:
Range
covers the entire range including headers.*Hint: If you're targeting specific cells/columns/etc., make sure you have the referenced workbook in focus.
Make a random student selector
First, get the number of students by counting the rows.
Generate a random number between 1 and
StudentsCount
.Rnd
generates a random decimal number between 0 and 1.Rnd
byStudentsCount
, and we get a random decimal number between 1 and 64.999*.+ 1
to allow it to go up to 65.Int
to round it to an integer.RandomIndex
.Select a random student row from the table.