Software Training Institute in Chennai with 100% Placements – SLA Institute
⭐ Exclusive Summer Courses Offer ⭐ 💰 Flat ₹5,000 - ₹10,000 off on all courses 👨‍👩‍👧 Additional discounts for group enrollments 🎓 100% Placement Support 🏆 90,000+ Students Successfully Placed 🚀 Avail now! Limited seats only!
Vba Macros Tutorial For Beginners - Softlogic Systems
Share on your Social Media

VBA Macros Tutorial for Beginners

Published On: October 10, 2024

Introduction

Are you wasting time in Excel performing some repetitive, tedious tasks? Do you find recorded macros rigid, or do you get stuck deep inside trying to read some cryptic VBA code? You’re not alone! This tutorial demystifies VBA and turns what took hours of manual work into seconds of automated efficiency. Begin creating solutions on your own today.

Ready for the blueprint to mastering automation? Download the complete VBA Macros course syllabus now!

Why Students or Freshers Learn VBA Macros?

The reasons for students or freshers should learn VBA Macros:

  • Automation of Repetitive Tasks: One can definitely automate all those time-consuming, daily tasks that one does within Excel-like formatting, cleaning data, or generating reports, using VBA. This automation greatly improves personal productivity and saves hours.
  • High Business Demand: It is a primary skill for professions in the roles of Financial Analysis, Business Intelligence, Accounting, and Operations, all of which have to process big data with much speed.
  • Building Custom Solutions: You can develop user forms, reports, and complex tools that go well beyond ordinary Excel functions, making you an asset for any team.
  • Easy Start to Programming: VBA is fairly simple and offers an excellent, easy-to-enter opportunity for students of every profile to get the basics of programming (loops, conditions, variables) without complicated configurations.

Ready to land a position in automation? Get a copy of our VBA Macros Interview Questions and Answers today!

Check your knowledge level with our smart Knowledge Assessment Tool

  • Instant skill evaluation with accurate scoring
  • Identify strengths and learning gaps easily
  • Designed for students and working professionals
  • Smart assessment to guide your career growth

Take Your Eligibility Report Instantly

Step-by-Step VBA Macros Tutorial for Beginners

In the following comprehensive VBA Macros tutorial for beginners, you will learn how to set up your Excel environment and then write your very first piece of automation code. It also takes you from manually clicking through spreadsheets to leveraging the power of macros.

Step 1: Installation and Setup-Enabling the Developer Tab

Because VBA is already integrated into Microsoft Office, there is nothing to install for Excel, Word, and PowerPoint. Your job, however is to turn on the tools that you need to work with macros:

1.1: Activating the Developer Tab

The Developer Tab is the key to recording, viewing, and editing VBA code. It is hidden by default.

  1. Open Microsoft Excel.
  2. Click the File tab in the ribbon.
  3. Choose Options (usually at the bottom).
  4. Click Customize Ribbon on the left side of the Excel Options dialog box.
  5. Under the Main Tabs list on the right-hand side scroll down and select the checkbox beside Developer
  6. Click OK. You should now see the Developer tab in your main ribbon of Excel.

1.2: Save as a Macro-Enabled Workbook

If you write code and save the file as normal (.xlsx), the macros will be deleted! You have to save the workbook as a special format.

  • Click File → Save As.
  • In the “Save as type” dropdown menu, choose Excel Macro-Enabled Workbook (*.xlsm).
  • Name your workbook for instance MyFirstMacro and click on Save.

Step 2: Opening the Visual Basic Editor (VBE)

VBE stands for Visual Basic Editor. It is an integrated development environment, or IDE, where you will write and manage all your VBA code.

2.1: Open the VBE

You have two ways to open the VBE:

  • Click the Developer tab and click the Visual Basic button on the extreme left.
  • Use the keyboard shortcut: ALT + F11.

2.2: Know the VBE Interface

The VBE opens in a separate window and has four main components: If they are not visible, use the View menu to show them:

  1. Project Explorer (Ctrl+R): Lists all open workbooks and their components viz. Sheets, ThisWorkbook, and Modules.
  2. Properties Window F4: It opens the property window of the selected component, showing the name of the sheet for example.
  3. Code Window: The large central area where you actually write your VBA code.
  4. Immediate Window (Ctrl + G): to quickly test single lines, get values, and generally debug.

2.3: Adding a Module

All general-purpose macros (the most common type) are stored inside a Module.

  • Click your workbook, for example VBAProject (MyFirstMacro.xlsm), that appears in the Project Explorer window.
  • Click the Insert menu and choose Module.
  • A new folder, called Modules, has been added in the Project Explorer. Within this folder is Module1. Double-click Module1 to open the blank Code window.

Step 3: Writing Your First Macro (Sub Procedure)

In VBA, a Macro is more formally called a Sub Procedure, short for Subroutine. It’s a collection of statements that perform some action.

3.1: Write the Basic Form

Type the following lines into the Code Window for Module1:

Sub HelloWorld()

End Sub

  • Sub: Identifies the start of a process.
  • HelloWord: The name you want to call your macro.
  • (): Required, even if you don’t pass any arguments.
  • End Sub: Indicates the end of the procedure.

3.2: Introduce the MsgBox The simplest command

The MsgBox command shows a pop-up message box to the user.

Sub HelloWorld()

    ‘ This line displays a simple message box to the user.

    MsgBox “Hello, VBA World! I’ve been automated!”

End Sub

3.3 Running the Macro

With your cursor anywhere inside the HelloWorld code:

  • Click the Run Sub/UserForm (small green triangle) button on the VBE toolbar.
  • Alternatively, press F5.
  • Now go back to your Excel spreadsheet (ALT + F11) and you should see the message box pop up! Click OK.

Step 4: Working with the Worksheet: The Object Model

VBA’s true power is in its interaction with the Excel objects. VBA views Excel as a hierarchy of objects: Application → Workbook → Worksheet → Range.

4.1: Writing a Value to a Cell

The most frequent task – changing of the cell’s value – is made through the Range object.

Sub WriteToCell()

    ‘ Set the value of cell A1 on the active sheet

    Range(“A1”).Value = “Automated Report Start”    

    ‘ Set the value of cell B1

    Range(“B1”).Value = Date ‘ The built-in VBA function to get today’s date

End Sub

4.2 Cell Formatting

You are able to access the properties of the cell, like its font color or interior color.

Sub FormatCell()

    ‘ Select the cell we want to format

    Range(“A1”).Select

    ‘ Change the background color to yellow (ColorIndex 6)

    Selection.Interior.ColorIndex = 6

    ‘ Change the font to bold

    Selection.Font.Bold = True

End Sub

Note: This is inefficient as using .Select is unnecessary after having used .Selection. Instead, the range should be directly referenced: Range(“A1”).Font.Bold = True.

4.3: Data Copying

This macro copies a value and pastes it as a value, not as a formula, to avoid errors.

Sub CopyData()

    ‘ 1. Copy the data from cell A1

    Range(“A1”).Copy    

    ‘ 2. Paste the data as Values into cell C5 on a different sheet

    ‘ Use Sheets(“Sheet Name”) to refer to a specific sheet

    Sheets(“Sheet2”).Range(“C5”).PasteSpecial Paste:=xlPasteValues

    ‘ 3. Turn off the “marching ants” copy mode

    Application.CutCopyMode = False

End Sub

Step 5: Introduction of Variables and Control Structures

To create powerful, dynamic macros, information must be stored (in variables) and execution flow controlled through loops and conditions.

5.1: Declaring Variables

Variables hold data. It is always good practice and efficient to declare them through the use of the Dim statement.

Sub UseVariables()

    ‘ Dim (Dimension) declares the variable name and type

    Dim strName As String

    Dim lngRow As Long ‘ Use Long for row numbers (up to 2 billion)

    ‘ Assign values to the variables

    strName = “Monthly Summary”

    lngRow = 10

    ‘ Use the variables to output a value

    Cells(lngRow, 1).Value = strName ‘ Cells(Row, Column) is a great alternative to Range(“A1”)

End Sub

5.2: The If…Then…Else Conditional

This executes code only if a condition is met.

Sub CheckValue()

    Dim dblInput As Double

    ‘ Get the value from cell A1 (assuming it’s a number)

    dblInput = Range(“A1”).Value

    If dblInput > 100 Then

        MsgBox “Value exceeds the limit!”, vbCritical

    Else

        MsgBox “Value is acceptable.”, vbInformation

    End If

End Sub

vbCritical and vbInformation are built-in VBA constants that change the icon in the message box.

5.3: The For.Next Loop

Used for the repetition of a block of code a specified number of times, very useful when iterating over rows or columns.

Sub AddSerialNumbers()

    Dim i As Integer ‘ The loop counter

    ‘ Loop from row 1 to row 10

    For i = 1 To 10

        ‘ Write the value of ‘i’ into the current row of Column A

        Cells(i, 1).Value = i 

    Next i

    MsgBox “Serial numbers 1-10 added to Column A.”

End Sub

6. Saving and Running the Macro

6.1: Assigning the Macro to a Button (Control)

Rather than press F5, create a button to which your macro is assigned: this way, users can click it easily.

  1. Go to the Developer tab.
  2. Within the Controls group, click Insert.
  3. Under Form Controls, click the Button control (the first icon).
  4. Click and drag a box on your spreadsheet to draw the button.
  5. The Assign Macro dialog box opens. Highlight AddSerialNumbers (or whatever you called the macro you created) and select OK.
  6. Right click the button and choose “Edit Text” to change the label, such as to “Generate Serials”.
  7. Click outside the button. Now, one click runs your code!

You have successfully set up your environment, learned how to write and run basic VBA code, and mastered the essential concepts of the Excel Object Model, variables, and loops. You now can automate repetitive tasks!

The next logical progression in this tutorial is the application of these concepts through complex solutions that require logical thinking and problem-solving. Practice is the only way to set in concrete your skills and truly be able to call yourself an automation expert in Excel. Download our exclusive VBA Macros Challenges and Solutions

Real Time Examples for VBA Macros for Learners

The following scenarios will help to better translate VBA code into immediate business value: 

Automated Data Cleanup and Standardization

  • Objective: A macro to clean up messy imported data, such as from a CSV or an external database across a multi-sheet document. 
  • Concepts Learned: Utilization of For Each loops to iterate across all cells within a range, application of conditions If/Then/Else for either blank values or wrong formatting. Also, string manipulation functions like trimming extra space and using appropriate functions to normalize capitalization. 
  • Real-World Application: Cleaning raw data in order to prepare it for analysis is considered a daily investment on the part of finance and operations teams. 

Custom Report Creation Based on User Input

  • Objective: A macro that will prompt the user to select a report month and department ID, filter the main datasheet accordingly, and export the result into a new, formatted PDF or email body. 
  • Concepts Learned: Designing a basic UserForm to accept input, the InputBox function, applying the AutoFilter method on a Range object, and utilizing built-in methods to export to PDF or to work with Outlook. 
  • Real-World Application: Instantly produce personalized, standardized reports and save hours of manual filtering and formatting. 

Dynamic Dashboard Button Toggles

  • Objective: To provide a dashboard where various buttons are clicked in order to show or hide certain rows or columns, or toggle between different charts. 
  • Concepts Learned: Attaching macros to Form Controls, utilizing the Hidden property of Rows or Columns objects, and using simple logical flags or variables to keep track of the current state of a toggle. 
  • Real-World Application: To provide user-friendly, interactive dashboards in Excel regardless of complex pivot table slicers. 

Ready to start coding these powerful automation projects? Get our list of advanced VBA Macros project ideas, with step-by-step functionality requirements! 

FAQs About VBA Macros Tutorial for Beginners

1. Is VBA Macro easy to learn? 

Yes, VBA or Visual Basic for Applications is rated as relatively easy for beginners, especially for those who are familiar with Excel. Because of the relatively simple syntax it uses, an integrated debugger, and a feature called the Macro Recorder, it can get people up to speed with basic principles of programming such as loops, conditions, and how to work with the Excel Object Model in next to no time.

2. How to Learn VBA Macros? 

First, learn the Excel Object Model-the relationship between Excel objects like Workbooks and Ranges. Then learn how code is generated via the Macro Recorder, followed by custom coding in the VBE using basic concepts like variables, loops, and conditional statements. 

3. How do I write a simple VBA Macro?

Open the Visual Basic Editor via ALT+F11, insert a Module, declare a procedure: Sub MyFirstMacro(). In here, write the commands that would usually interact with Excel: Range(“A1”).Value = “Hello”. To run the macro, press F5 or assign to a button. 

4. Is VBA still useful in 2025?

Yes, VBA is indeed still very useful in 2025, especially within finance, accounting, and smaller-to-medium business settings. Though there are newer tools, with VBA integrated directly into Excel, and ease of use for rapid prototyping and automating repetitive and custom tasks keeps the toolset relevant. 

5. Is Microsoft replacing VBA? 

While Microsoft itself is not exactly replacing VBA, it’s promoting Office Scripts based on TypeScript for automation on the web and Power Automate Desktop for broader process automation. VBA, however, is still the most powerful desktop automation tool for Excel. 

6. How many hours to learn VBA?

You can learn the basics and be able to write simple, functioning macros in about 10-20 hours. Proficiency for more complicated projects may require 50-100 hours of intensive work by building projects with practical applications and learning the Excel Object Model. 

7. Is VBA different than SQL?

Yes, VBA and SQL are altogether different. VBA is a procedural language used in the automation of tasks inside Microsoft Office applications, mainly Excel. SQL, or Structured Query Language, is a declarative language used in managing and retrieving data from relational databases.

8. Is Excel VBA like Python? 

Well, they are not similar in syntax, but both are used for automation and data analysis. Python is a general-purpose, powerful language across many domains. VBA is strictly tied to Microsoft Office. Normally, Python is preferred for large-scale and complex analysis. 

9. Which programming language is closest to VBA? 

The language closest to VBA is VB.Net. VBA is based on older Visual Basic 6. Thus, the syntax and object-oriented principles involved in VBA and VB.Net are similar. It is, therefore, rather easy to switch from VBA to VB.Net or VB6. 

10. What is the salary of VBA expert?

The salary for a professional with strong VBA expertise varies greatly by role and industry since it’s often a supplemental skill. However, roles that require advanced VBA for financial analysis or data automation usually range between $65,000 and $110,000 USD, depending on experience and location. 

Conclusion

You have installed and configured the Developer tab, you have been oriented to the VBE, and you have written your first functional macros with loops and conditionals. You now have a good foundation to stop doing much of the repetitive, tedious work in Excel manually. It’s now time to apply those skills in challenging, real-world reporting and data integration projects. Real proficiency is achieved by practice, and tackling some of the more advanced topics remaining, such as UserForms and interfacing with other Office applications. Build professional automation tools and transform your productivity today with our Advanced VBA Macro Course in Chennai.

Share on your Social Media
Get Your Instant Job & Placement Eligibility
Report in Just 30 Seconds!
Below 30% - not Eligible (Needs Preparation)
30% – 70% - Partially Eligible (Needs Guidance)
Above 70% - Fully Eligible (Ready to Start)

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.