In Excel, macros can be created by recording a sequence of actions using the built-in macro recorder to automate repetitive tasks. They can be simple or complex depending on the actions they're intended to perform. Macros are a way to automate tasks without necessarily needing to write VBA code manually.
VBA is the abbreviation for Visual Basic for Applications, and it is a programming language that is used in various Microsoft applications such as Excel, Word, Access, and others. It allows users to write code to automate tasks, create custom functions, and perform a wide range of operations. VBA is a powerful tool for automating repetitive tasks and extending the functionality of Microsoft Office programs.
VBA is the underlying programming language to create custom automation and functionality while a macro is a specific instance of automation created by recording user actions. Macros are often used to simplify repetitive tasks, and VBA offers a more comprehensive and powerful way to create, edit, and manage code.
- VBA Terms:
- VBA Editor: The VBA editor is the place where we start writing codes. To open the VBA editor, you can either click the "Visual Basic" in the "Developer" tab or use the shortcut "Alt + F11".
- Modules: The module is the container for VBA codes. You can write and edit codes within the modules.
- VBA Objects: Excel exposes a hierarchy of objects that you can manipulate using VBA, such as
Workbook
,Worksheet
,Range
etc.
- VBA Codes
Sub VariableExample()
' Declare variables
Dim myInteger As Integer
Dim myString As String
Dim myDouble As Double
' Assign values
myInteger = 10
myString = "Hello, VBA!"
myDouble = 3.14
' Display values in a message box
MsgBox "Integer: " & myInteger & vbCrLf & "String: " & myString & vbCrLf & "Double: " & myDouble
End Sub
- The VBA codes: The VBA codes always start with "Sub" and end with "End Sub".
- Declare variables: In the VBA codes, you can declare variables using "Dim" followed by the variable name and the data type (e.g., Dim myInteger As Integer). "Dim" means dimension or Declare in Memory in VBA.
- Comment out: For the notes or codes you do not want to run, you can comment out using the apostrophe sign (e.g., ' Assign values).