Create, run and record VBA macros
What is a macro?
Macros are special programs that will help you auto-execute specific tasks instead of performing manual tasks repeatedly. Visual Basic for Applications (VBA) is the programming language used to write macros. In Zoho Sheet, you can use the VBA editor to create, record, run, and manage macros.
To create a macro:
- Go to Tools > VBA Macros > Create Macro.
- In the Create Macro dialog that appears, give the preferred macro name and description.
- Once done, click Create.
- In the VBA Editor, you can write the macro code
- Once done, click on Save.
Sample macro codes to get started with:
Hide and unhide row
- Sub HideUnhideRowColumn()
- Sheets("Sheet1").Columns("A:E").Hidden = True
- Sheets("Sheet1").Columns("C:D").Hidden = False
- Sheets("Sheet1").Rows("11:15").Hidden = True
- Sheets("Sheet1").Rows("12:13").Hidden = False
- End Sub
Cut, copy and paste rows
- Private Sub CopyCutRow()
- WorkSheets("Sheet1").Rows(2).Copy WorkSheets("Sheet1").Rows(4) //copy row 2 and paste it to row 4
- WorkSheets("Sheet2").Rows(2).Cut WorkSheets("Sheet1").Rows(4)//cut row 2 and paste it to row 4
- End Sub
Send emails based on the change in cell value
- Private Sub Worksheet_Change(ByVal Target As Range)
- Set watchRange = Range("B1:B100") 'Assuming cell range to be monitored is
- If Not Intersect(Target,watchRange) Is Nothing Then
- If Target.Value = "Send Mail" Then
- ThisWorkbook.HasRoutingSlip = True
- With ThisWorkbook.RoutingSlip
- .Recipients = Array("SendEmail@zoho.com") 'Can send mail to multiple recipients
- .Subject = "Task completed"
- .Message = "Mail content" & Target.Row 'Sending cell value in mail
- .AttachWorkbook = True
- 'By default the file will be attached in the mail. If it is not needed then send
- ' .AttachWorkbook as false.
- End With
- ThisWorkbook.Route
- End If
- End If
- End Sub
Workbook open event and new sheet create event
- REM ThisWorkbook Module
- Private Sub Workbook_Open()
- Msgbox("I am a open event ",-1,"Welcome back!")
- End Sub
- Private Sub Workbook_NewSheet(ByVal sh As Object)
- Msgbox("I am a new sheet event , You have created a sheet ! ",-1," Name it ")
- End Sub
Worksheet change event
- REM Sheet Module
- Private Sub Worksheet_Change(ByVal Target As Range)
- Application.EnableEvents = false
- On Error Goto ENEV
- REM add your code below
- Msgbox(" I am a change event ",-1,"OH thats a good change!")
- ENEV:
- Application.EnableEvents = true
- End Sub
To record a macro:
- Go to Tools > VBA Macros > Record Macro.
- In the Record Macro dialog that appears, enter the name of the macro you wish to record. You can check or uncheck "Use Relative Reference" based on your preference.
- Once done, click on Record.
- To stop recording the macro, click on the (stop icon). Further, you can click on View Code to view the recorded macro code in the VBA Editor. Or you can click on Macro List to view the list of macros you've created to date.

Absolute referencing - The cells are referenced $Column_Number$Cell_Number, meaning the macro is executed in the same cells used when recording the macro.
Relative referencing - The cells are referenced without the "$" constraint, meaning the macro is executed with cells relative from where the macro is executed.