Learn Excel Macros with Examples, in this section you will find useful macros for beginner to advanced users. You can also find the basic to advanced topics which are properly explained with number of examples and downloadable files.

Learn Macros with Examples – 15 Basic Macros for Absolute Beginners

Learning Basic Excel VBA By Examples is the easiest way to understand the basics of VBA to deal with Excel Objects, in this tutorial we will not covering any programming concepts, we will see how to access the different Excel Object using VBA.

Download the Example File Here:

ANALYSIS TABS – 15 Examples VBA Codes for Bigenners

Example 1: How To Access The Excel Range And Show The Value Using Message Box

'Example 1: How To Access The Excel Range And Show The Value Using Message Box
Sub sbExample1()
    
    'It will display the A5 value in the message Box
    MsgBox Range("A5")
    
    'You can also use Cell Object to refer A5 as shwon below:
     MsgBox Cells(5, 1) 'Here 5 is Row number and 1 is Column number

End Sub


Example 2: How To Enter Data into a Cell

'Example 2: How To Enter Data into a Cell
Sub sbExample2()
    
    'It will enter the data into B5
    Range("B5") = "Hello World! using Range"
    
    'You can also use Cell Object as shwon below B6:
    Cells(6, 2) = "Hello World! using Cell" 'Here 6 is Row number and 2 is Column number

End Sub


Example 3: How To Change The Background Color Of A Particular Range

'Example 3: How To Change The Background Color Of A Particular Range
Sub sbExample3()
    
    'You can set the background color using Interior.ColorIndex Property
    Range("B1:B5").Interior.ColorIndex = 5 ' 5=Blue

End Sub


Example 4: How To Change The Font Color And Font Size Of A Particular Range

'Example 4: How To Change The Font Color And Font Size Of A Particular Range
Sub sbExample4()
    
    'You can set the background color using Interior.ColorIndex Property
    Range("B1:B10").Font.ColorIndex = 3 ' 3=Red

End Sub


Example 5: How To Change The Text To Upper Case Or Lower Case

'Example 5: How To Change The Text To Upper Case Or Lower Case
Sub sbExample5()
    
    'You can use UCase finction to change the text into Upper Case
    Range("C2").Value = UCase(Range("C2").Value)
    
     'You can use LCase finction to change the text into Upper Case
    Range("C3").Value = LCase(Range("C3").Value)
    

End Sub


Example 6: How To Copy Data From One Range To Another Range

'Example 6: How To Copy Data From One Range To Another Range
Sub sbExample6()
    
    'You can use Copy method
    Range("A1:D11").Copy Destination:=Range("H1")

End Sub


Example 7: How To Select And Activate Worksheet

'Example 7: How To Select And Activate Worksheet
Sub sbExample7()
    
    'You can use Select Method to select
    Sheet2.Select
    
    'You can use Acctivate Method to activate
    Sheet1.Activate

End Sub


Example 8: How To Get The Active Sheet Name And Workbook Name

'Example 8: How To Get The Active Sheet Name And Workbook Name
Sub sbExample8()
    
    'You can use ActiveSheet.Name property to get the Active Sheet name
    MsgBox ActiveSheet.Name
    
    'You can use ActiveWorkbook.Name property to get the Active Workbook name
    MsgBox ActiveWorkbook.Name

End Sub


Example 9: How To Add New Worksheet And Rename A Worksheet and Delete Worksheet

'Example 9: How To Add New Worksheet And Rename A Worksheet and Delete Worksheet
Sub sbExample9()
    
    'You can use Add method of a worksheet
    Sheets.Add
    
    'You can use Name property of worksheet
    ActiveSheet.Name = "Temporary Sheet"
   
   'You can use Delete method of a worksheet
    Sheets("Temporary Sheet").Delete

End Sub


Example 10: How To Create New Workbook, Add Data, Save And Close The Workbook

'Example 10: How To Create New Workbook, Add Data, Save And Close The Workbook
Sub sbExample10()
    
    'You can use Add method of a Workbooks
    Workbooks.Add
    
    'You can use refer parent and child object to access the range
    ActiveWorkbook.Sheets("Sheet1").Range("A1") = "Sample Data"
   
   'It will save in the deafult folder, you can mention the full path as "c:TempMyNewWorkbook.xls"
    ActiveWorkbook.SaveAs "MyNewWorkbook.xls"
    
    ActiveWorkbook.Close
    
End Sub


Example 11: How To Hide And Unhide Rows And Columns

'Example 11: How To Hide And Unhide Rows And Columns
Sub sbExample11()
    
    'You can use Hidden Propery of Rows
    Rows("12:15").Hidden = True 'It will Hide the Rows 12 to 15
    
    Rows("12:15").Hidden = False 'It will UnHide the Rows 12 to 15
    
    
    'You can use Hidden Propery of Columns
    Columns("E:G").Hidden = True 'It will Hide the Rows E to G
    
    Columns("E:G").Hidden = False 'It will UnHide the Rows E to G
   
    
End Sub


Example 12: How To Insert And Delete Rows And Columns

'Example 12: How To Insert And Delete Rows And Columns
Sub sbExample12()
    
    'You can use Insert and Delete Properties of Rows
    Rows(6).Insert 'It will insert a row at 6 row
    
    Rows(6).Delete 'it will delete the row 6
    
    
    'You can use Insert and Delete Properties of Columns
    Columns("E").Insert 'it will insert the column at E
    
    Columns("E").Delete 'it will delete the column E
    
End Sub


Example 13: How To Set The Row Height And Column Width

'Example 13: How To Set The Row Height And Column Width
Sub sbExample13()
    
    'You can use Hidden Propery of Rows
    Rows(12).RowHeight = 33
    
    Columns(5).ColumnWidth = 35
    
End Sub


Example 14: How To Merge and UnMerge Cells

'Example 14: How To Merge and UnMerge Cells
Sub sbExample14()
    
    'You can use Merge Property of range
    Range("E1:E5").Merge
    
    'You can use UnMerge Property of range
    Range("E1:E5").UnMerge
        
End Sub


Example 15:

How To Compare Two Values – A Simple Example On If Condition

How To Print 1000 Values – A Simple Example On For Loop

'Example 15:
'How To Compare Two Values – A Simple Example On If Condition
'How To Print 1000 Values – A Simple Example On For Loop
Sub sbExample15()
    
'A Simple Example On If Condition
'We will campare A2 and A3 value using If
If Range("A2").Value = Range("A3") Then
    MsgBox "True"
Else
    MsgBox "False"
End If

'A Simple Example On For Loop
'We will print 1- 1000 integers in the Column E

For i = 1 To 1000
    Cells(i, 5) = i 'here 5= column number of E
Next i

'showing status once it is printed the 1000 numbers
MsgBox "Done! Printed 1000 integers in Column E"

End Sub
For more Example Codes Refer:

Most Useful Excel VBA Tips (100+ Most useful VBA Codes)

Excel Macros and VBA for Beginners:

This section is for beginners with no knowledge of Excel Macros or VBA. By end of this session you will be able to Record and Run a Macro and be able to accept the Inputs from the user and show the Popup Messages.
Read more…

Topics:

» Introduction to VBA
» Excel Macros in Nutshell: Getting Started with Excel VBA Macros
» VBA Environment
» Recording a Macro
» Writing Your First VBA Macro- Hello World!
» Debugging VBA Code
» Accepting Values (Input Box) & Popup Messages (Message Box)
» Modifying and Commenting Macros or VBA Code
» Executing a Macro
» Macro Security
» Learn Macros with Examples – 15 Basic Macros for Absolute Beginners

Excel VBA Programming:

In this section you will learn the VBA programming from the basics to write your own VBA programs like sub routines, functions to automate your daily tasks. By end of this session you will be able to write the procedures and functions to automate tasks and you will find significant time saving while working with your projects.
Read more…

Topics:

» Getting Started with VBA Programming
» Understanding VBA Object Model
» Working with Excel Objects – Workbook, Sheets, Range and Cell
» Variables and Data Types
» Operators and Operands
» Conditional Statements
» Sub Procedures

Advanced VBA Programming:

This section is for advanced programmers who are comfortable with the above concepts. In this section you will learn how to develop the Forms and interact with the other applications, handling the files and other advanced programming concepts. By end of this sessions you will be comfortable with VBA and be confident to develop the tools to automate complex tasks and complex Dashboards.

Read more…

Topics:

» Scope of the Variables
» Passing Arguments
» Arrays
» Introduction to Collections
» UserForms and Controls
» Events
» Objects, Properties and Methods
» VBA Security
» Introduction to ADO and SQL : Connecting to a database with VBA
» Interacting with Other Applications