Sarah's VB.NET Playground
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Find my other projects on
- GitHub
- itch.io
- YouTube
VB.NET / VBA Basics
VB.NET & VBA are case insensitive meaning you can write functions & variables in lower case or upper case.
This means if we create a variable 'dim i as integer' we can also refer to this variable as 'I'.
VB.NET & VBA are similar but not the same, read more about the differences here
HELLO WORLD
1
2
3
4
5
6
7
8
Imports System
Public Module modmain
Sub Main()
Console.WriteLine ("Hello World!") 'In VBA you can use 'Debug.Print()' instead of 'Console.WriteLine'
End Sub
End Module
Enter to Rename, Shift+Enter to Preview
IF
1
2
3
4
5
6
7
8
9
10
11
12
Imports System
Public Module modmain
Sub Main()
DIM CONDITION AS BOOLEAN: CONDITION = TRUE
IF CONDITION THEN
Console.WriteLine ("THIS IS " & CONDITION)
END IF
End Sub
End Module
Enter to Rename, Shift+Enter to Preview
What is the variable 'CONDITION' in the code above?
ELSE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Imports System
Public Module modmain
Sub Main()
DIM CONDITION AS BOOLEAN: CONDITION = TRUE
IF CONDITION THEN
Console.WriteLine ("THIS IS " & CONDITION)
ELSE
Console.WriteLine ("THIS IS " & (CONDITION = TRUE))
END IF
End Sub
End Module
Enter to Rename, Shift+Enter to Preview
SELECT CASE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Imports System
Public Module modmain
Sub Main()
DIM VALUE AS STRING: VALUE = "RED"
SELECT CASE VALUE
CASE "GREEN"
Console.WriteLine ("VALUE IS GREEN")
CASE "YELLOW"
Console.WriteLine ("VALUE IS YELLOW")
CASE "RED"
Console.WriteLine ("VALUE IS RED")
CASE "BLUE"
Console.WriteLine ("VALUE IS BLUE")
CASE ELSE
Console.WriteLine ("VALUE NOT THE DEFAULT COLOUR")
END SELECT
End Sub
End Module
Enter to Rename, Shift+Enter to Preview
WHILE
1
2
3
4
5
6
7
8
9
10
11
12
13
Imports System
Public Module modmain
Sub Main()
DIM NUMBER AS INTEGER: NUMBER = 10
DIM COUNTER AS INTEGER: COUNTER = 1
WHILE COUNTER <= NUMBER
Console.WriteLine ("COUNTING - " & COUNTER)
COUNTER = COUNTER + 1
END WHILE 'in VBA this should be 'WEND' instead of 'END WHILE'
End Sub
End Module
Enter to Rename, Shift+Enter to Preview
FOR
1
2
3
4
5
6
7
8
9
10
11
Imports System
Public Module modmain
Sub Main()
DIM NUMBER AS INTEGER: NUMBER = 10
FOR COUNTER AS INTEGER = 1 TO NUMBER STEP 1 'STEP 1 IS NOT NEEDED
Console.WriteLine ("COUNTING - " & COUNTER)
NEXT
End Sub
End Module
Enter to Rename, Shift+Enter to Preview
FOR EACH
1
2
3
4
5
6
7
8
9
10
11
Imports System
Public Module modmain
Sub Main()
DIM NUMBERS(10) AS INTEGER: NUMBERS = NEW INTEGER() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
FOR EACH I AS INTEGER IN NUMBERS
Console.WriteLine ("COUNTING - " & I)
NEXT
End Sub
End Module
Enter to Rename, Shift+Enter to Preview
Advanced usage
If you want a more complex example (external libraries, viewers...), see the official documentation.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content