VBA Macros

VBA Macros

How to create a Macro

To create a macro:
  1. Navigate to Developer > Create Macro
  2. In the dialog that appears, enter a name and a description for the macro, then click Create.
  3. Enter the code in the VBA editor.
  4. Click 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

How to record a macro

Use macros to automate everyday tasks. If you are to apply the same formatting everyday, you can record macros. 

To record a macro:
  1. Go to Developer > Record Macros 
  2. Enter a name and a description in the input field.
  3. You can check or uncheck Use Relative Reference as needed.
  4. Click Record.
  5. To stop recording, click on the Stop icon 
  6. Click View Code to view the recorded macro in the VBA editor or click Macro List to view all the macros in the spreadsheet.
Notes
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

                                TypeformWCAG


                                      All-in-one knowledge management and training platform for your employees and customers.

                                                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

                                                                                                                                  • VBA Macros

                                                                                                                                    How to create a Macro To create a macro: Navigate to Developer > Create Macro In the dialog that appears, enter a name and a description for the macro, then click Create. Enter the code in the VBA editor. Click Save. Sample macro codes to get started ...
                                                                                                                                  • 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 ...
                                                                                                                                  • Configure and enable OpenAI in Zoho Sheet

                                                                                                                                    ChatGPT by OpenAI is a generative artificial intelligence tool that processes the given queries and provides detailed responses. Zoho Sheet's integration with OpenAI will enhance the way you analyze your spreadsheet data with advanced AI features. ...
                                                                                                                                  • 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 ...
                                                                                                                                  • How to insert a button

                                                                                                                                    How to insert a button To insert a button: Go to Developer > Button Right-click on the button and select Edit Label from the contextual menu to rename it. Alternatively, you can double-click on the button to name it. How to assign an existing macro ...
                                                                                                                                    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