Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Copy All Macros Given Below to Excel – VB – Sheet 1 – General

Statements

Then press excel button on the top left

Click on Macros in Developer Tab

You can view all macros

Go to options Give Shortcut key for all 15– Shift + A & so on

Press Shortcut key to run the macro


Excel VBA Macros for Beginners- Novice Example 1: How To Access
The Excel Range And Show The Value Using Message Box

While automating most of the Excel Tasks, we need to read the data from Excel spread
sheet range and perform some calculations. This example will show you how to read the
data from a worksheet range.

'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

Excel VBA Macros for Beginners- Novice Example 2: How To Enter


Data into a Cell

After performing some calculations using VBA, we generally write the results into
worksheet ranges. This example will show you how to write the data from VBA to Spread
sheet range or 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

Excel VBA Macros for Beginners- Novice Example 3: How To Change


The Background Color Of A Particular Range

The following example will help you in formatting a cell or range by changing the
background color of a range. We can use ColorIndex property of a Ranger Interior object
to change the fill color of a range or cell.
'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

Excel VBA Macros for Beginners- Novice Example 4: How To Change


The Font Color And Font Size Of A Particular Range

You may need to change the font color of range or cell sometimes. We can differentiate
or highlight the cell values by changing the text color of range in the worksheet. The
following method will use the font ColorIndex property of a range to change the font
color.

'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

Excel VBA Macros for Beginners- Novice Example 5: How To Change


The Text To Upper Case Or Lower Case

This example will help you to change the text from lower case to upper case. We use
UCase Function to do this. If you wan tot change the text or string from upper case to
lower case, we can use LCase function.

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


Sub sbExample5()

'You can use UCase function to change the text into Upper Case
Range("C2").Value = UCase(Range("C2").Value)

'You can use LCase function to change the text into Upper Case
Range("C3").Value = LCase(Range("C3").Value)

End Sub

Excel VBA Macros for Beginners- Novice Example 6: How To Copy


Data From One Range To Another Range
This example will help you to copy the data from one particular range to another range in
a worksheet using VBA.

'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

Excel VBA Macros for Beginners- Novice Example 7: How To Select


And Activate Worksheet

This example will help you to select one particular worksheet. To activate one particular
sheet we can use Activate method of a 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

Excel VBA Macros for Beginners- Novice Example 8: How To Get The
Active Sheet Name And Workbook Name

We can use Name property of a active sheet to get the worksheet name. We can use
name property of active workbook to get the name of the active workbook.

'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
Excel VBA Macros for Beginners- Novice Example 9: How To Add
New Worksheet And Rename A Worksheet and Delete Worksheet

We can Name property of worksheet to rename or change the worksheet name. We can
use Add method of worksheets to add a new worksheet. Use Delete method to delete a
particular 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

Excel VBA Macros for Beginners- Novice Example 10: How To Create
New Workbook, Add Data, Save And Close The Workbook

The following examples will help you to adding some data, saving the file and closing the
workbook after saving 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:\Temp\MyNewWorkbook.xls"
ActiveWorkbook.SaveAs "MyNewWorkbook.xls"

ActiveWorkbook.Close

End Sub
Excel VBA Macros for Beginners- Novice Example 11: How To Hide
And Unhide Rows And Columns

We can use hidden property of rows or columns of worksheet to hide or unhide the rows
or 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

Excel VBA Macros for Beginners- Novice Example 12: How To Insert
And Delete Rows And Columns

This example will show you how to insert or delete the rows and columns using VBA.

'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

Excel VBA Macros for Beginners- Novice Example 13: How To Set The
Row Height And Column Width

We can set the row height or column width using VBA. The following example will show
you how to do this using VBA.
'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

Excel VBA Macros for Beginners- Novice Example 14: How To Merge
and UnMerge Cells

Merge method of a range will help you to merge or unmerge the cell of range using VBA.

'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

Excel VBA Macros for Beginners- Novice Example 15: How To


Compare Two Values – A Simple Example On If Condition

When dealing with Excel VBA, often we compare the values. A Simple Example On If
Condition will help you to understand -How To Compare Two Values?

'Example 15:
'How To Compare Two Values – A Simple Example On If Condition
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
End Sub
Excel VBA Macros for Beginners- Novice Example 16: How To Print
1000 Values – A Simple Example On For Loop

Another useful statement in VBA is For, helps to loop through the rows, columns or any
number of iteration. See the belwo example to understand the For loop:

Sub sbExample15()
'How To Print 1000 Values – A Simple Example On For Loop

'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

Sub sbExample16()
'How To Print 1000 Values - A Simple Example On For Loop
Dim i As Integer

For i = 1 To 1000
i = i + 1
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

OR

Sub sbExample16()
Dim i As Integer
Dim j as integer
J=1000
For i = 1 To 1000
Cells(j, 5) = j 'here 5= column number of E
J = j - 1
Next i
MsgBox "Done! Printed 1000 integers in Column E"

End Sub

You might also like