Variable in Excel VBA | Part 6

preview_player
Показать описание
Variable in Excel VBA:
Requirement: Microsoft Excel 2019

In VBA (Visual Basic for Applications) for Excel, variables are used to store data that can be manipulated during program execution.
Here's a quick overview of how to declare and use variables in VBA:
Declaring Variables:
You can declare variables using the Dim statement. You can specify the type of variable (e.g., Integer, String, Double) to optimize memory usage.
Dim myInteger As Integer
Dim myString As String
Dim myDouble As Double

Setting Values:
myInteger = 10
myString = "Hello, World!"
myDouble = 3.14

Example:
Sub TotalPrice()
Dim total As Double
Dim price As Double
Dim quantity As Integer
price = 9.99
quantity = 5
total = price * quantity
MsgBox "Total Price: " & total
End Sub

Variable Scope:
Local Variables: Declared within a procedure and only accessible within that procedure.
Module-level Variables: Declared at the top of a module (outside any procedures) and accessible by all procedures within that module.
Global Variables: Declared using the Public keyword at the top of a module and accessible from any module in the project.
Рекомендации по теме