Visual Basic for Applications โ the macro language built into every copy of Excel, Word, and Access. It's how millions of office workers and analysts automate spreadsheets without ever calling themselves "programmers," and it remains one of the most quietly-used languages in the business world.
Sub HelloWorld()
MsgBox "Hello, world!"
End Sub
Sub blocks and are triggered by a button, keyboard shortcut, or the Excel Macros menu โ not run from a command line.Dim age As Integer
age = 25
Dim name As String
name = "Sam"
Dim price As Double
price = 19.99
If age >= 18 Then
MsgBox "Adult"
ElseIf age >= 13 Then
MsgBox "Teen"
Else
MsgBox "Child"
End If
Dim i As Integer
For i = 1 To 5
MsgBox i
Next i
Dim n As Integer
n = 3
Do While n > 0
MsgBox n
n = n - 1
Loop
VBA's real day job โ reading, writing, and manipulating spreadsheet data directly.
Range("A1").Value = "Total"
Range("B1").Value = 100
Dim total As Double
total = Range("B1").Value + Range("B2").Value
Dim lastRow As Integer
lastRow = Cells(Rows.Count, 1).End(xlUp).Row ' find the last used row
Same split as classic VB โ Sub does something, Function returns a value you can use in a formula or another macro.
Function Add(a As Integer, b As Integer) As Integer
Add = a + b
End Function