Professional Documents
Culture Documents
Excel VBA Macros for Beginners
Excel VBA Macros for Beginners
Statements
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()
End Sub
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.
End Sub
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()
End Sub
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()
End Sub
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.
'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
End Sub
This example will help you to select one particular worksheet. To activate one particular
sheet we can use Activate method of a worksheet.
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
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()
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 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.
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.
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()
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.
End Sub
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()
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
For i = 1 To 1000
Cells(i, 5) = i 'here 5= column number of E
Next i
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
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