You are on page 1of 22

Write Data to Worksheet Cell in Excel VBA

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.

Write Data to Worksheet Cell in Excel VBA – Solution(s):

It is same as reading the data from Excel to VBA. We can use Cell or Range Object to write into a Cell.

Write Data to Worksheet Cell in Excel VBA – An Example of using Cell


Object
The following example will show you how to write the data to Worksheet Cell using Cell Object.

Example Codes

In this example I am writing the data to first Cell of the Worksheet.

Sub sbWriteIntoCellData()

Cells(1, 1)="Hello World"


'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 writing the data to first row and fourth column of the worksheet.

Sub sbWriteIntoCellData1()

Cells(1, 4)="Hello World"

End Sub

Write Data to Worksheet Cell in Excel VBA – An Example of using Range


Object
The following example will show you how to write the data into Worksheet Cell or Range using Range Object.

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

Write Data to Worksheet Cell in Excel VBA – Specifying the Parent


Objects
When you are writing the data using Cell or Range object, it will write the data into Active Sheet. If you want to
write the data to another sheet, you have to mention the sheet name while writing the data.

The below example is reading the data from Range A5 of Sheet2:

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.

Writing and Reading Excel Worksheet Cells Ranges in VBA

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.

Writing and Reading Excel Worksheet Cells Ranges in VBA –


Solution(s):

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.

Example1 : Reading and Writing the data using Cell Object


In this example I am reading the data from Range B3 and Writing the data into C5 using Cell Object.

Sub sbReadWriteCellExample1()

'Using Cell Object


Cells(5, 3) = Cells(3, 2)
MsgBox Cells(5, 3)
End Sub

Example2 : Reading and Writing the data using Range Object

In this example I am reading the data from Range B3 and Writing the data into C5 using Range Object.

Sub sbReadWriteCellExample2()

'Using Range Object


Range("C5") = Range("B3")
MsgBox Range("C5")

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()

'Using Cell and Range Object


Cells(5,3) = Range("B3")
MsgBox Cells(5,3)

'OR
'Range("C5")=Cells(3,2)
'MsgBox Range("C5")

End Sub

Example4 : Reading and Writing the data – From different Worksheets


We can mention the Sheet name while reading the data from another sheet.

Sub sbReadWriteCellExample3()

'Using Cell and Range Object


Cells(5,3) = Sheets("Sheet5").Range("B3")
MsgBox Cells(5,3)

End Sub

Example4 : Reading and Writing the data – Write to different Worksheets

We can mention the Sheet name while Writing the data to another sheet.

Sub sbReadWriteCellExample3()

'Using Cell and Range Object


Sheets("Sheet5").Cells(5,3) = Range("B3")

MsgBox Sheets("Sheet5").Cells(5,3)

End Sub

Similarly, we can use the workbook names, if you want to write into different workbooks.

Read or Get Data from Worksheet Cell to VBA in Excel

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.

Read or Get Data from Worksheet Cell to VBA in Excel –


Solution(s):
It is very simple to read the data from Excel to VBA. We can use Cell or Range Object to refer a Worksheet
Cell.

Get Data from Worksheet Cell – An Example of using Cell Object


The following example will show you how to read or get the data from Worksheet Cell using Cell Object.

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.

Get Data from Worksheet Cell – An Example of using Range Object


The following example will show you how to read or get the data from Worksheet Cell or Range using Range
Object.

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.

Get Data from Worksheet Cell – Specifying the Parent Objects


When you are reading using Cell or Range object, it will read the data from Active Sheet. If you want to read
the data from another sheet, you have to mention the sheet name while reading the data.

The below example is reading the data from Range A5 of Sheet2:

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.

Select Cell Range in Excel VBA

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.

Select Cell Range in Excel VBA – Solution:

We can use Select method of Range or Cell to select it and do whatever you want to do.

Select Cell Range in Excel VBA – Example:


Following are the various examples which will show you how to select a range and perform some task.

Example to Select a Cell

In this example I am selecting a Cell using Select method of Cell.

Sub sbSelectACell()

'Selecting a Cell
Cells(2, 3).Select 'This will select the Cell at 2nd row and 3rd column

End Sub

Example to Select a Range


In this example I am selecting a Range using Select method of Range.

Sub sbSelectARange()
'You can also use Range Object
Range("C3").Select

'Collection of Cells OR Multiple Cells = Range


Range ("B2:C4").Select ' It will Select B2,B3,B4,C2,C3,C4

End Sub

Example to Select a Range and change the background color

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

1. Open an Excel Workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a Module from Insert Menu
4. Copy the above code and Paste in the code window
5. Save the file as macro enabled workbook
6. Press F5 to execute it

Copy Data from One Range to Another in Excel VBA

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

Copy Data from One Range to Another in Excel VBA- Solution(s):

You can use Copy method of a range to copy the data from one range to another range.

Copy Data from One Range to Another in Excel VBA – An Example

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:

1. Open an excel workbook


2. Enter some data in Sheet1 at A1:B10
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. Press F5 to run it

Now you should see the required data (from Range A1 to B10 ) is copied to the target range (Range E1 to F10).

Clear Cells in Excel Range Worksheet using VBA

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

Clear Cells in Excel of a range or Worksheet using VBA- Solution(s):

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.

Clear Cells Range data in Excel Worksheet using VBA – An Example

The following examples will show you how clear the data of Cells, Range or entire worksheet using Clear and
ClearContents Methods.

Clearing a Cells/Range using Clear Method

This method will clear the range of cells including Formats:

Sub sbClearCells()
Range("A1:C10").Clear
End Sub

Clearing Only Data of a Range using ClearContents Method

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

Clearing Entire Worksheet using Clear Method

This method will clear entire worksheet including formats.

Sub sbClearEntireSheet()
Sheets("SheetName").Cells.Clear
End Sub

Clearing Only Data from Worksheet using ClearContents Method

This method will clear only data of worksheet, not formats.

Sub sbClearEntireSheetOnlyData()
Sheets("SheetName").Cells.ClearContents
End Sub

Instructions:

1. Open an excel workbook


2. Enter some data in Sheet1 at A1:C10
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. Press F5 to run it

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.

Change Font Color in Excel VBA


Description:
We usually change the font color if we want to highlight some text. If we want to tell the importance of some
data, we highlight the text to get the user attention to a particular range in the worksheet. For examples we can
change the font color of highly positive figures in to Green or all negative figures in to red color. So that user
can easily notice that and understand the data.

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…

Change Font Color in Excel VBA – Solution(s):


We can change the font color by using Font.ColorIndex OR Font.Color Properties of a Range/Cell.

Change Font Color in Excel VBA – Examples


The following examples will show you how to change the font color in Excel using VBA.

'In this Example I am changing the Range B4 Font Color


Sub sbChangeFontColor()

'Using Cell Object


Cells(4, 2).Font.ColorIndex = 3 ' 3 indicates Red Color

'Using Range Object


Range("B4").Font.ColorIndex = 3

'--- You can use use RGB, instead of ColorIndex -->


'Using Cell Object
Cells(4, 2).Font.Color = RGB(255, 0, 0)

'Using Range Object


Range("B4").Font.Color = RGB(255, 0, 0)

End Sub

Instructions:

1. Open an excel workbook


2. Enter some data in Ranges mentioned above
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. Press F5 to execute itit
Here is an example screen-shot for changing Font Colors:

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.

Change Font to Bold in Excel VBA


Description:
Some ties we need to change highlight the font to show its importance. We Change Font to Bold or change the
background color or font color in Excel VBA, to highlight the text or some numbers. So that reader can easily
recognize and look into that.

Change Font to Bold in Excel VBA – Solution:

We can change the font to Bold by using Font.Bold Property of a Range/Cell in Excel using VBA.

Change Font to Bold in Excel VBA – Examples:


The following examples will show you how to change the font to bold in Excel using VBA.

'In this Example I am changing the Range B4 Font to Bold


Sub sbChangeFontToBold()

'Using Cell Object


Cells(4, 2).Font.Bold = True

'Using Range Object


Range("B4").Font.Bold = True

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.

Change Text Case – Upper Lower in Excel VBA


Description:
We can automate task to Change Text Case – Upper Lower in Excel VBA. See the following examples to know
how to do this.

Change Text Case – Upper Lower in Excel VBA: Solution

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:

Follow the below instructions to do it yourself.

1. Open an excel workbook


2. Add some text at A3 and A4 ranges
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. Press F5 to execute it

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) <> ""

If Cells(iCntr, 1) = Cells(iCntr, 2) Then


Cells(iCntr, 3) = "Matched"
Else
Cells(iCntr, 3) = "Not Matched"
End If

iCntr = iCntr + 1
Loop
End Sub

You can see the output in Column C:

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) <> ""

If UCase(Cells(iCntr, 1)) = UCase(Cells(iCntr, 2)) Then


Cells(iCntr, 3) = "Matched"
Else
Cells(iCntr, 3) = "Not Matched"
End If

iCntr = iCntr + 1
Loop
End Sub

You can see the output in Column C is different than the above result.

Change Background Color of Cell Range in Excel VBA

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.

Change Background Color of Cell Range in Excel VBA –


Solution(s):

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()

'Using Cell Object


Cells(3, 2).Interior.ColorIndex = 5 ' 5 indicates Blue Color

End Sub

Example 2

In this Example below I am changing the Range B3 Background Color using Range Object

Sub sbRangeFillColorExample2()

'Using Range Object


Range("B3").Interior.ColorIndex = 5

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)

'Using Range Object


Range("B3").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:

1. Open an excel workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a new module from Insert menu
4. Copy the above code and Paste in the code window
5. Save the file as macro enabled workbook
6. Press F5 to execute the procedure
7. You can see the interior colors are changing as per our code

Change Background Color of Cell Range in Excel VBA – Download:


Example File
Here is the sample screen-shot of the example file.
Merge UnMerge Cell Range in Excel VBA

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.

Merge UnMerge Range in Excel VBA – Solution:

We can use Merge and UnMerge methods of a Range to Merge UnMerge Cell Range in Excel VBA.

Merge UnMerge Cell Range in Excel VBA – Example:


Following is the example to show you how to merge or UnMerge the cells in excel using VBA.

Code to merge Cells

Sub sbMergeRange()

Range("A1:B3").Merge

End Sub

Code to unmerge Cells

Sub sbUnMergeRange()

Range("A1:B3").UnMerge

End Sub

Instructions:

1. Open an excel workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a Module for Insert Menu
4. Copy the above code and Paste in the code window
5. Save the file as macro enabled workbook
6. Press F5 to execute it
Add Clear Comments in Excel VBA

Description:
Comments are helpful when you want to show some remarks or comments on particular cell, you can Add Clear
Comments in Excel VBA.

Add Clear Comments in Excel VBA – Solution:


You can use AddComment and ClearComments methods to do this.

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:

1. Open an excel workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a Module for Insert Menu
4. Copy the above code and Paste in the code window
5. Save the file as macro enabled workbook
6. Press F5 to execute it

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.

Following is the Example program to add the comments from a range.

Sub sbAddComment_Example()
For iCntr = 1 To 30
'Clear if any existing comments
Range("A3").ClearComments

'Add a Comment from Column B


Range("A" & iCntr).AddComment
Range("A" & iCntr).Comment.Text Text:=Range("B" & iCntr).Value

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:

1. Open an excel workbook


2. Enter some data in Column A and B as per your requirement, to execute the above program you need to
enter some data from Range A1 to Range B30.
3. Press Alt+F11 to open VBA Editor
4. Insert a Module for Insert Menu
5. Copy the above code and Paste in the code window
6. Save the file as macro enabled workbook
7. Press F5 to execute it

You might also like