Examples of Excel macros written with VBA

A comprehensive collection of phone data for research analysis.
Post Reply
sumaiyakhatun26
Posts: 378
Joined: Sun Dec 22, 2024 8:32 am

Examples of Excel macros written with VBA

Post by sumaiyakhatun26 »

Ercole Palmeri 2024-03-25
Excel Tutorial
The following simple Excel macro examples were written using VBA.
Table of Contents
VBA example using arrays
VBA example with mathematical operations
VBA example with modification date recording
Estimated reading time: 3 minutes

VBA example using arrays
The following subroutine reads values ​​from cells in column A of the active qatar rcs data worksheet until it encounters an empty cell. The values ​​are stored in an array. This simple Excel macro example illustrates its use:

Variable declarations;
Dynamic array;
A cycle Do Until;
Refer to cells in the current Excel worksheet;
VBA function Ubound builtin (which returns the highest index of an array).
VBA example with modification date recording
Let's write a simple VBA macro that fires when a cell in a specific range of our sheet is updated. Suppose you want to track changes in column B (B4 to B11) and record the date and time of the change in column A.
Let's proceed like this:

DeveloperClick on the option in the tab " Visual Basicto open the VBA Editor.
In the VBA Editor, double-click the code editor related to Sheet2.
Select the worksheet from the right (or left) tab and choose the Change option.
Add VBA code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1:B10")) Is Nothing Then
Target.Range("A1:A1").Value = Now
End If
End Sub
Save the workbook with macros enabled (for example, as an .xlsm file).


Now, whenever we update a cell in column B (row 1 to row 10), the cell in column A will automatically display the current date and time.
Post Reply