Create, run, and record VBA macros | Zoho Sheet Help Guide

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:

  1. Go to Tools > VBA Macros > Create Macro.
  2. In the Create Macro dialog that appears, give the preferred macro name and description.
  3. Once done, click Create.
  4. In the VBA Editor, you can write the macro code
  5. Once done, click on Save.

Sample macro codes to get started with:

Hide and unhide row

  1. Sub HideUnhideRowColumn()
  2.     Sheets("Sheet1").Columns("A:E").Hidden = True
  3.     Sheets("Sheet1").Columns("C:D").Hidden = False
  4.     Sheets("Sheet1").Rows("11:15").Hidden = True
  5.     Sheets("Sheet1").Rows("12:13").Hidden = False
  6. End Sub

Cut, copy and paste rows

  1. Private Sub CopyCutRow()
  2.     WorkSheets("Sheet1").Rows(2).Copy WorkSheets("Sheet1").Rows(4) //copy row 2 and paste it to row 4
  3.     WorkSheets("Sheet2").Rows(2).Cut WorkSheets("Sheet1").Rows(4)//cut row 2 and paste it to row 4
  4. End Sub

Send emails based on the change in cell value

  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Set watchRange = Range("B1:B100") 'Assuming cell range to be monitored is
  3. If Not Intersect(Target,watchRange) Is Nothing Then
  4.   If Target.Value = "Send Mail" Then
  5.     ThisWorkbook.HasRoutingSlip = True
  6.     With ThisWorkbook.RoutingSlip
  7.         .Recipients = Array("SendEmail@zoho.com") 'Can send mail to multiple recipients
  8.         .Subject = "Task completed"
  9.         .Message = "Mail content" & Target.Row 'Sending cell value in mail
  10.         .AttachWorkbook = True
  11.         'By default the file will be attached in the mail. If it is not needed then send
  12.         ' .AttachWorkbook as false.
  13.     End With
  14.     ThisWorkbook.Route
  15.     End If
  16. End If
  17. End Sub


Workbook open event and new sheet create event

  1. REM ThisWorkbook Module

  2. Private Sub Workbook_Open()
  3. Msgbox("I am a open event ",-1,"Welcome back!")
  4. End Sub
  5. Private Sub Workbook_NewSheet(ByVal sh As Object)
  6. Msgbox("I am a new sheet event , You have created a sheet !  ",-1," Name it ")
  7. End Sub


Worksheet change event

  1. REM Sheet Module

  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3. Application.EnableEvents = false
  4. On Error Goto ENEV
  5. REM add your code below
  6. Msgbox(" I am a change event ",-1,"OH thats a good change!")
  7. ENEV:
  8. Application.EnableEvents = true
  9. End Sub

To record a macro:

  1. Go to Tools > VBA Macros > Record Macro.
  2. 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.
  3. Once done, click on Record.
  4. 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.


Info
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.


      Create. Review. Publish.

      Write, edit, collaborate on, and publish documents to different content management platforms.

      Get Started Now


        Access your files securely from anywhere

          Zoho CRM Training Programs

          Learn how to use the best tools for sales force automation and better customer engagement from Zoho's implementation specialists.

          Zoho CRM Training
            Redefine the way you work
            with Zoho Workplace

              Zoho DataPrep Personalized Demo

              If you'd like a personalized walk-through of our data preparation tool, please request a demo and we'll be happy to show you how to get the best out of Zoho DataPrep.

              Zoho CRM Training

                Create, share, and deliver

                beautiful slides from anywhere.

                Get Started Now


                  Zoho Sign now offers specialized one-on-one training for both administrators and developers.

                  BOOK A SESSION







                              Quick LinksWorkflow AutomationData Collection
                              Web FormsEnterpriseOnline Data Collection Tool
                              Embeddable FormsBankingBegin Data Collection
                              Interactive FormsWorkplaceData Collection App
                              CRM FormsCustomer ServiceAccessible Forms
                              Digital FormsMarketingForms for Small Business
                              HTML FormsEducationForms for Enterprise
                              Contact FormsE-commerceForms for any business
                              Lead Generation FormsHealthcareForms for Startups
                              Wordpress FormsCustomer onboardingForms for Small Business
                              No Code FormsConstructionRSVP tool for holidays
                              Free FormsTravelFeatures for Order Forms
                              Prefill FormsNon-Profit

                              Intake FormsLegal
                              Mobile App
                              Form DesignerHR
                              Mobile Forms
                              Card FormsFoodOffline Forms
                              Assign FormsPhotographyMobile Forms Features
                              Translate FormsReal EstateKiosk in Mobile Forms
                              Electronic Forms
                              Drag & drop form builder

                              Notification Emails for FormsAlternativesSecurity & Compliance
                              Holiday FormsGoogle Forms alternative GDPR
                              Form to PDFJotform alternativeHIPAA Forms
                              Email FormsFormstack alternativeEncrypted Forms

                              Wufoo alternativeSecure Forms

                              WCAG



                                        Create. Review. Publish.

                                        Write, edit, collaborate on, and publish documents to different content management platforms.

                                        Get Started Now







                                                          You are currently viewing the help pages of Qntrl’s earlier version. Click here to view our latest version—Qntrl 3.0's help articles.




                                                              Manage your brands on social media


                                                                • Desk Community Learning Series


                                                                • Digest


                                                                • Functions


                                                                • Meetups


                                                                • Kbase


                                                                • Resources


                                                                • Glossary


                                                                • Desk Marketplace


                                                                • MVP Corner


                                                                • Word of the Day


                                                                • Ask the Experts


                                                                  Zoho Sheet Resources

                                                                   

                                                                      Zoho Forms Resources


                                                                        Secure your business
                                                                        communication with Zoho Mail


                                                                        Mail on the move with
                                                                        Zoho Mail mobile application

                                                                          Stay on top of your schedule
                                                                          at all times


                                                                          Carry your calendar with you
                                                                          Anytime, anywhere




                                                                                Zoho Sign Resources

                                                                                  Sign, Paperless!

                                                                                  Sign and send business documents on the go!

                                                                                  Get Started Now




                                                                                          Zoho TeamInbox Resources





                                                                                                    Zoho DataPrep Demo

                                                                                                    Get a personalized demo or POC

                                                                                                    REGISTER NOW


                                                                                                      Design. Discuss. Deliver.

                                                                                                      Create visually engaging stories with Zoho Show.

                                                                                                      Get Started Now









                                                                                                                          • Related Articles

                                                                                                                          • Create a new blank spreadsheet

                                                                                                                            To create a new blank spreadsheet, open the app and follow these steps: In the listing page, tap the + icon in the bottom bar. Choose the Blank Spreadsheet option from the listed gallery.
                                                                                                                          • Supported VBA modules in Zoho Sheet

                                                                                                                            Classes Properties Methods <Globals> All properties and modules under VBA are globals (i.e., they can be used directly without prefixing the respective module name) All properties and modules under VBA are globals (i.e., they can be used directly ...
                                                                                                                          • Create a new spreadsheet

                                                                                                                            To create a new spreadsheet: 1. Visit the file listing page and tap Blank Spreadsheet.
                                                                                                                          • Create a named range

                                                                                                                            To create a named range: Select the desired cell range. Go to  Under the Insert menu, choose Define Name. You can choose to define a cell range, an expression, or a constant value. Enter the desired values and tap Done.
                                                                                                                          • Create spreadsheets from templates

                                                                                                                            To create a new spreadsheet from templates, open the app and follow these steps: In the listing page, tap the + icon in the bottom bar. Simply tap to open the desired template from the default template gallery. You can immediately start editing the ...
                                                                                                                            Wherever you are is as good as
                                                                                                                            your workplace

                                                                                                                              Resources

                                                                                                                              Videos

                                                                                                                              Watch comprehensive videos on features and other important topics that will help you master Zoho CRM.



                                                                                                                              eBooks

                                                                                                                              Download free eBooks and access a range of topics to get deeper insight on successfully using Zoho CRM.



                                                                                                                              Webinars

                                                                                                                              Sign up for our webinars and learn the Zoho CRM basics, from customization to sales force automation and more.



                                                                                                                              CRM Tips

                                                                                                                              Make the most of Zoho CRM with these useful tips.



                                                                                                                                Zoho Show Resources