Professional Documents
Culture Documents
Description:
In the previous post we have seen, how to read data from excel to VBA. We will see how to write data to
Worksheet Cell in Excel VBA.
It is same as reading the data from Excel to VBA. We can use Cell or Range Object to write into a Cell.
Example Codes
Sub sbWriteIntoCellData()
In this example I am writing the data to first row and fourth column of the worksheet.
Sub sbWriteIntoCellData1()
End Sub
Example Codes
In this example I am reading the data from first Cell of the worksheet.
Sub sbRangeData()
Range("A1")="Hello World"
'Here you have to specify the Cell Name which you want to read - A is the Column and 1 is the Row
End Sub
Sub sbRangeData1()
Sheets("Sheet2").Range("A5")="Hello World"
'Here the left side part is sheets to refer and the right side part is the range to read.
End Sub
In the same way you can mention the workbook name, if you are writing the data to different workbooks.
Description:
While working with Excel it is common task to writing and reading the Workrksheet. For example, we may
have a Input data sheet to enter data, based on the data in Input sheet we can perform some calculations and
show the results to the user.
We can use Cell or Range Object as discussed in the previous topics to read and write the using VBA. The
following example will show you how write and read the data using Excel VBA.
Sub sbReadWriteCellExample1()
In this example I am reading the data from Range B3 and Writing the data into C5 using Range Object.
Sub sbReadWriteCellExample2()
End Sub
Example3 : Reading and Writing the data using Cell and Range Object
Even we can use combination of Cell and Range. In this example I am reading the data from Range B3 and
Writing the data into C5 using Cell and Range Object.
Sub sbReadWriteCellExample3()
'OR
'Range("C5")=Cells(3,2)
'MsgBox Range("C5")
End Sub
Sub sbReadWriteCellExample3()
End Sub
We can mention the Sheet name while Writing the data to another sheet.
Sub sbReadWriteCellExample3()
MsgBox Sheets("Sheet5").Cells(5,3)
End Sub
Similarly, we can use the workbook names, if you want to write into different workbooks.
Description:
Its one of my first questions when I started learning EXcel VBA, How to Read or Get Data from Worksheet
Cell to VBA? To automate any thing in excel, first we need to read the data in Worksheet and based on that we
can execute the next steps.
Example Codes
In this example I am reading the data from first Cell of the worksheet.
Sub sbGetCellData()
MsgBox Cells(1, 1)
'Here the first value is Row Value and the second one is column value
'Cells(1, 1) means first row first column
End Sub
In this example I am reading the data from first row and fourth column of the worksheet.
Sub sbGetCellData1()
MsgBox Cells(1, 4)
End Sub
Here is sample picture, which helps you to understand this concepts. Any Row or Column number start with 1
and You have to specify Cell(RowNumber,ColumnNumber) to read the data from a Cell of the Worksheet.
Example Codes
In this example I am reading the data from first Cell of the worksheet.
Sub sbGetCellData2()
MsgBox Range("A1")
'Here you have to specify the Cell Name which you want to read - A is the Column and 1 is the Row
End Sub
Here is sample picture, which helps you to understand this concepts. If you select any cell in the worksheet, you
can see the name of that cell in the Name Box.
Sub sbGetCellData2()
MsgBox Sheets("Sheet2").Range("A5")
'Here the left side part is sheets to refer and the right side part is the range to read.
End Sub
In the same way you can mention the workbook name, if you are reading the data from different workbooks.
Description:
If we want to deal with Cell or Range we can directly mention the object and change its properties or call its
methods. We can also deal with Cells or Range by simply selecting it. Then we can use Selection object to refer
Selected Cells, Range or any other Objects.
We select cell range in Excel VBA to do particular task with the selection. For example we may want to change
the background color of a range and we may want to change the font to Bold. In this case we do not required to
loop each and every cell of the range to change the background color and font.
We can use Select method of Range or Cell to select it and do whatever you want to do.
Sub sbSelectACell()
'Selecting a Cell
Cells(2, 3).Select 'This will select the Cell at 2nd row and 3rd column
End Sub
Sub sbSelectARange()
'You can also use Range Object
Range("C3").Select
End Sub
In this example I am selecting the range from B2:C4 using Select method. And changing the background color
to red using ColorIndex Property of Range.
Sub sbSelectARangeandForamt()
'Selecting a Range
Range("B2:C4").Select ' It will Select B2,B3,B4,C2,C3,C4
Selection.Interior.ColorIndex = 3
End Sub
Instructions
Description:
Copying Data from One Range to Another range is most commonly performed task in Excel VBA
programming. We can copy the data, Formats, Formulas or only data from particular range in Excel Workbook
to another range or Sheet or Workbook.
This example to show you how to copy data from one Range to another using Excel VBA. This is one of the
frequently used codes in the VBA, we often do this activity, copying the data from one range to another range in
a worksheet’s
You can use Copy method of a range to copy the data from one range to another range.
The following example will show you copying the data from one range to another range in a worksheet using
Excel VBA.
Code:
'In this example I am Copying the Data from Range ("A1:B10") to Range(E1")
Sub sbCopyRange()
'Method 1
Range("A1:B10").Copy Destination:=Range("E1")
'Here the first part is source range,
'and the second part is target range or destination.
'Target can be either one cell or same size of the source range.
'Method 2
Range("A1:B10").Copy
Range("E1").Select
ActiveSheet.Paste
'In the second method, first we copy the source data range
'Then we select the destination range
'Target can be either one cell or same size of the source range.
'Then we paste in the active sheet, so that it will paste from active range
Application.CutCopyMode = False
End Sub
Instructions:
Now you should see the required data (from Range A1 to B10 ) is copied to the target range (Range E1 to F10).
Description:
Most of the times we clear the data from a cells or a range and re-enter to do some calculations. For examples
we may have some template to enter data and calculate the tax. We may want to do this for all the employees of
an organization. In this case we need to Clear data Excel from a Range in Worksheet using VBA before
entering the data for each employee
We can clear Cells or a Range using Clear Method OR ClearContents Method of a Range or Cell. Clear
will Clear the data and Formats of the given Range or Cells. And ClearContents will clear only the data, will not
clear any formats.
The following examples will show you how clear the data of Cells, Range or entire worksheet using Clear and
ClearContents Methods.
Sub sbClearCells()
Range("A1:C10").Clear
End Sub
This method will clear only clear the content or data of the range not formats (Formats remain same)
Sub sbClearCellsOnlyData()
Range("A1:C10").ClearContents
End Sub
Sub sbClearEntireSheet()
Sheets("SheetName").Cells.Clear
End Sub
Sub sbClearEntireSheetOnlyData()
Sheets("SheetName").Cells.ClearContents
End Sub
Instructions:
Conclusion:
Both Clear and ClearContents are useful based on your requirement. If you want to Clear only the Content, use
ClearContent method. If you want Clear everything (Content and Formats), use Clear method.
In this topic we will see how change the font color in Excel VBA. Again, we change the font color in excel
while generating the reports. We may want to highlight the font in red if the values are negative, in green if the
values are positive. Or we may change font colors and sizes for headings. etc…
End Sub
Instructions:
We can change the font color while working with the reports. But it is good practice to limit to only few colors,
instead of using many colors in a single report. In also need to mantian the same color format while delivering
the same kind of report next time.
See the following example screen-shot, we are using the same font and background colors for in ranges in the
worksheet. It looks good with same king of colors, instead of using multiple colors.
We can change the font to Bold by using Font.Bold Property of a Range/Cell in Excel using VBA.
End Sub
When we want to highlight some text in the Excel, we alsoe consider changing the Fornt and Background
Colors. We use ColorIndex or RGB format of Colors to apply the color for both Font and Background . You can
learn more here:
Also find related example to change font Color in Excel using VBA
In this topic we will see how change the font color in Excel VBA. Again, we change the font color in excel
while generating the reports. We may want to highlight the font in red if the values are negative, in green if the
values are positive. Or we may change font colors and sizes for headings. etc…
You can find multiple examples on changing the font colors in Excel using VBA
Find the related example to change the background color in Excel using VBA:
It is an interesting feature in excel, we can change background color of Cell, Range in Excel VBA. Specially,
while preparing reports or dashboards, we change the backgrounds to make it clean and get the professional
look to our projects.
You can find multiple examples on changing the Background colors in Excel using VBA.
We can use UCase and LCase function to change a text into Upper and Lower.
Examples
Following are the examples to show you how to do this practically.
Sub sbChangeCASE()
'Upper Case
Range("A3") = UCase(Range("A3"))
'Lower Case
Range("A4") = LCase(Range("A4"))
End Sub
Instructions:
Practical Applications:
Most of the times we use UCASE and LCASE function while comparing the text. For examples if we have two
Columns and want to compare the Cells of each columns to check if they are same.
Let’s assume the following data is there in Column A and B, and we want to compare it and print it in Column
C.
A B
Apples apples
Banana Bananaa
Red RED
GREE
Green
n
pink pink
If you write a procedure to compare this data without using UCASE or LCASE function, we may not get the
desired results.
See the following Example code and output. We can observe that the its comparing but its case sensitive.
Sub sbCompareColumns_1()
iCntr = 1
Do While Cells(iCntr, 1) <> ""
iCntr = iCntr + 1
Loop
End Sub
I am using the same code with UCASE function, so that first it will convert to uppercase then compare it. We
can avoid the case sensitivity.
Sub sbCompareColumns_2()
iCntr = 1
Do While Cells(iCntr, 1) <> ""
iCntr = iCntr + 1
Loop
End Sub
You can see the output in Column C is different than the above result.
Description:
It is an interesting feature in excel, we can change background color of Cell, Range in Excel VBA. Specially,
while preparing reports or dashboards, we change the backgrounds to make it clean and get the professional
look to our projects.
We can use Interior.Color OR Interior.ColorIndex properties of a Rage/Cell to change the background colors.
Change Background Color of Cell Range in Excel VBA – Examples
The following examples will show you how to change the background or interior color in Excel using VBA.
Example 1
In this Example below I am changing the Range B3 Background Color using Cell Object
Sub sbRangeFillColorExample1()
End Sub
Example 2
In this Example below I am changing the Range B3 Background Color using Range Object
Sub sbRangeFillColorExample2()
End Sub
Example 3
We can also use RGB color format, instead of ColorIndex. See the following example:
Sub sbRangeFillColorExample3()
'Using Cell Object
Cells(3, 2).Interior.Color = RGB(0, 0, 250)
End Sub
Example 4
The following example will apply all the colorIndex form 1 to 55 in Activesheet.
Sub sbPrintColorIndexColors()
Dim iCntr
For iCntr = 1 To 56
Cells(iCntr, 1).Interior.ColorIndex = iCntr
Cells(iCntr, 1) = iCntr
Next iCntr
End Sub
Instructions:
Description:
You can Merge UnMerge Cell Range in Excel VBA, it is required if you want to make bigger cells, instead of
existing cell to fit your data and needs.
We can use Merge and UnMerge methods of a Range to Merge UnMerge Cell Range in Excel VBA.
Sub sbMergeRange()
Range("A1:B3").Merge
End Sub
Sub sbUnMergeRange()
Range("A1:B3").UnMerge
End Sub
Instructions:
Description:
Comments are helpful when you want to show some remarks or comments on particular cell, you can Add Clear
Comments in Excel VBA.
Example:
The following code will show you how to add and clear comments using Excel VBA.
Code:
Sub sbAddComment()
'Deletes Existing Comments
Range("A3").ClearComments
'Creates Comment
Range("A3").AddComment
Range("A3").Comment.Text Text:="This is Example Comment Text"
End Sub
Instructions:
Practical Applications:
If you have lots of Cells to add comments, we general write the comments in another set of range and add using
VBA.
Sub sbAddComment_Example()
For iCntr = 1 To 30
'Clear if any existing comments
Range("A3").ClearComments
Next iCntr
End Sub
Explantion:
1. For Loop is to iterate from 1 to 30 rows, you can change as per your require mt
2. ClearComents method is using to clear the existing comments if any
3. AddComments method will add the comment in the particular range
4. Comment.Text property is for adding the Commet text or message which you want ot show it the user
on mose hover on a range
Instructions: