401 lines
5.9 KiB
Markdown
401 lines
5.9 KiB
Markdown
---
|
|
theme: gaia
|
|
_class: lead
|
|
paginate: true
|
|
backgroundColor: #fff
|
|
backgroundImage: url('./images/backdrop_09.jpg')
|
|
---
|
|
|
|

|
|
|
|
# **Excel VBA**
|
|
|
|
Excel VBA introduction
|
|
|
|
https://louiscklaw.github.io/
|
|
|
|
---
|
|
|
|
### VBA IDE
|
|
|
|

|
|
|
|
---
|
|
|
|
### Enable VBA IDE 1/2
|
|
|
|

|
|
|
|
---
|
|
|
|
### VBA Start IDE
|
|
|
|

|
|

|
|
|
|
---
|
|
|
|
### VBA - Save with macro enabled workbook 1/2
|
|
|
|

|
|

|
|
|
|
---
|
|
|
|
### VBA - Save with macro enabled workbook 2/2
|
|
|
|

|
|
|
|
---
|
|
|
|
### VBA - File menu
|
|
|
|

|
|
|
|
<!-- "run", "interrupt", "break point" -->
|
|
|
|
---
|
|
|
|
### VBA - Run menu
|
|
|
|

|
|
|
|
<!-- "run", "interrupt", "break point" -->
|
|
|
|
---
|
|
|
|
### VBA - Debug menu
|
|
|
|

|
|
|
|
<!-- "step into", "step over" -->
|
|
|
|
---
|
|
|
|
### VBA - Module
|
|
|
|

|
|

|
|
|
|
---
|
|
|
|
### VBA looping - For loop 1/2
|
|
|
|
```VB
|
|
For counter = start To end [Step stepcount]
|
|
[statement 1]
|
|
....
|
|
[statement n]
|
|
[Exit For]
|
|
[statement 11]
|
|
....
|
|
[statement n]
|
|
Next
|
|
```
|
|
|
|
---
|
|
|
|
<!-- please draft me a sample of VBA for loop -->
|
|
|
|
### VBA looping - For loop 2/2
|
|
|
|
```VB
|
|
Sub ForLoopExample()
|
|
Dim i As Integer
|
|
|
|
' Loop from 1 to 5
|
|
For i = 1 To 5
|
|
' Print the value of i
|
|
Debug.Print i
|
|
Next i
|
|
|
|
' Loop from 10 to 1 with a step of -1
|
|
For i = 10 To 1 Step -1
|
|
' Print the value of i
|
|
Debug.Print i
|
|
Next i
|
|
End Sub
|
|
```
|
|
|
|
---
|
|
|
|
### VBA looping - While loop 1/2
|
|
|
|
```
|
|
While condition(s)
|
|
[statements 1]
|
|
[statements 2]
|
|
...
|
|
[statements n]
|
|
Wend
|
|
```
|
|
|
|
---
|
|
|
|
<!-- please draft me a sample of VBA while loop -->
|
|
|
|
### VBA looping - While loop 2/2
|
|
|
|
```VB
|
|
Sub WhileLoopExample()
|
|
Dim i As Integer
|
|
|
|
' Loop while i is less than or equal to 5
|
|
i = 1
|
|
While i <= 5
|
|
' Print the value of i
|
|
Debug.Print i
|
|
|
|
' Increment i by 1
|
|
i = i + 1
|
|
Wend
|
|
|
|
' Loop while i is greater than 0
|
|
i = 10
|
|
Do While i > 0
|
|
' Print the value of i
|
|
Debug.Print i
|
|
|
|
' Decrement i by 1
|
|
i = i - 1
|
|
Loop
|
|
End Sub
|
|
```
|
|
|
|
---
|
|
|
|
### VBA - Functions 1/2
|
|
|
|
```VB
|
|
Function Functionname(parameter-list)
|
|
statement 1
|
|
.......
|
|
statement n
|
|
End Function
|
|
```
|
|
|
|
---
|
|
|
|
<!-- please draft me a sample of VBA function -->
|
|
|
|
### VBA - Functions 2/2
|
|
|
|
```VB
|
|
Function AddNumbers(num1 As Double, num2 As Double) As Double
|
|
' Add two numbers and return the result
|
|
AddNumbers = num1 + num2
|
|
End Function
|
|
|
|
Sub TestAddNumbers()
|
|
Dim result As Double
|
|
|
|
' Call the AddNumbers function and store the result in the 'result' variable
|
|
result = AddNumbers(5.5, 3.2)
|
|
|
|
' Display the result
|
|
MsgBox "The result is: " & result
|
|
End Sub
|
|
```
|
|
|
|
---
|
|
|
|
<!-- please draft me a sample of VBA Sub -->
|
|
|
|
### VBA - Subs 1/2
|
|
|
|
```VB
|
|
Sub Area(x As Double, y As Double)
|
|
MsgBox x * y
|
|
End Sub
|
|
```
|
|
|
|
---
|
|
|
|
<!-- please draft me a sample of VBA Sub -->
|
|
|
|
### VBA - Subs 2/2
|
|
|
|
```VB
|
|
Sub GreetUser(name As String)
|
|
' Display a greeting message with the provided name
|
|
MsgBox "Hello, " & name & "! Welcome to our program."
|
|
End Sub
|
|
|
|
Sub TestGreetUser()
|
|
' Call the GreetUser subroutine and pass a name
|
|
GreetUser "John"
|
|
End Sub
|
|
```
|
|
|
|
---
|
|
|
|
### VBA - Message Box
|
|
|
|
```VB
|
|
MsgBox(prompt[,buttons][,title][,helpfile,context])
|
|
```
|
|
|
|
TODO: screen capture of Msgbox
|
|
|
|
---
|
|
|
|
### VBA - Constants
|
|
|
|
```VB
|
|
Const MyInteger As Integer = 42
|
|
Const myDate As Date = #2/2/2020#
|
|
Const myDay As String = "Sunday"
|
|
```
|
|
|
|
### VBA - Variables
|
|
|
|
```VB
|
|
Dim MyInteger As Integer = 42
|
|
Dim myDate As Date = #2/2/2020#
|
|
Dim myDay As String = "Sunday"
|
|
```
|
|
|
|
---
|
|
|
|
### VBA - Arithmatic Operators
|
|
|
|
let A = 5, B = 10
|
|
|
|
| sign | means/equals |
|
|
| ---- | ---------------------- |
|
|
| \+ | A + B will give 15 |
|
|
| \- | A - B will give -5 |
|
|
| \* | A \* B will give 50 |
|
|
| / | B / A will give 2 |
|
|
| Mod | B Mod A will give 0 |
|
|
| ^ | B ^ A will give 100000 |
|
|
|
|
---
|
|
|
|
### VBA - Comparison Operators
|
|
|
|
let A = 5, B = 10
|
|
|
|
| sign | means/equals |
|
|
| ---- | ------------------ |
|
|
| = | (A = B) is False. |
|
|
| <> | (A <> B) is True. |
|
|
| \> | (A > B) is False. |
|
|
| < | (A < B) is True. |
|
|
| \>= | (A >= B) is False. |
|
|
| <= | (A <= B) is True. |
|
|
|
|
---
|
|
|
|
### VBA - If Statement 1/3
|
|
|
|
```VB
|
|
If(boolean_expression) Then
|
|
Statement 1
|
|
.....
|
|
.....
|
|
Statement n
|
|
End If
|
|
```
|
|
|
|
---
|
|
|
|
### VBA - If Elseif - Else statement 2/3
|
|
|
|
```VB
|
|
If(boolean_expression) Then
|
|
Statement 1
|
|
.....
|
|
Statement n
|
|
ElseIf (boolean_expression) Then
|
|
Statement 1
|
|
.....
|
|
Statement n
|
|
ElseIf (boolean_expression) Then
|
|
Statement 1
|
|
.....
|
|
Statement n
|
|
Else
|
|
Statement 1
|
|
.....
|
|
Statement n
|
|
End If
|
|
```
|
|
|
|
---
|
|
|
|
### VBA - If statement example 3/3
|
|
|
|
```VB
|
|
Sub CheckNumber(number As Integer)
|
|
' Check if the number is greater than 10
|
|
If number > 10 Then
|
|
' Display a message if the condition is true
|
|
MsgBox "The number is greater than 10."
|
|
Else
|
|
' Display a message if the condition is false
|
|
MsgBox "The number is not greater than 10."
|
|
End If
|
|
End Sub
|
|
```
|
|
|
|
---
|
|
|
|
<style scoped>
|
|
h6 { font-size: 18px; }
|
|
</style>
|
|
|
|
### VBA - Helloworld Example 1/2
|
|
|
|
```VB
|
|
Sub HelloWorld()
|
|
' Display a message box with "Hello, World!"
|
|
MsgBox "Hello, World!"
|
|
End Sub
|
|
|
|
Sub TestHelloWorld()
|
|
' Call the HelloWorld subroutine
|
|
HelloWorld
|
|
End Sub
|
|
```
|
|
|
|
###### Note to Louis: show debug, show run
|
|
|
|
---
|
|
|
|
### VBA - Helloworld Example 2/2
|
|
|
|

|
|
|
|
---
|
|
|
|
# VBA - Q n A / Thank you
|
|
|
|

|
|
|
|
## <!--
|
|
|
|
### VBA - Procedures
|
|
|
|
```VB
|
|
Procedures
|
|
```
|
|
|
|
---
|
|
|
|
### VBA - Procedures
|
|
|
|
```VB
|
|
Procedures
|
|
```
|
|
|
|
---
|
|
|
|
### VBA - Procedures
|
|
|
|
```VB
|
|
Procedures
|
|
```
|
|
|
|
--- -->
|