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.
A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.
Save Up to 85% LIMITED TIME OFFER
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates
Excel Pack
50+ Excel PM Templates
PowerPoint Pack
50+ Excel PM Templates
MS Word Pack
25+ Word PM Templates
Ultimate Project Management Template
Ultimate Resource Management Template
Project Portfolio Management Templates
Related Posts
-
- Description:
- Write Data to Worksheet Cell in Excel VBA – Solution(s):
- Write Data to Worksheet Cell in Excel VBA – An Example of using Cell Object
- Write Data to Worksheet Cell in Excel VBA – An Example of using Range Object
- Write Data to Worksheet Cell in Excel VBA – Specifying the Parent Objects
VBA Reference
Effortlessly
Manage Your Projects
120+ Project Management Templates
Seamlessly manage your projects with our powerful & multi-purpose templates for project management.
120+ PM Templates Includes:
29 Comments
-
Tamil
February 4, 2015 at 4:42 PM — ReplyHi Madam,
the way of tutor is awesome for Beginners. Thanks a lot
Edward Tamil
-
Waris
March 27, 2015 at 11:09 AM — ReplyHi Team,
I am just starting learning ABC of VBA macro, I hope it is very useful site for such learner like me….
Keep it…
Waris
-
JAVAID IQBAL
June 8, 2015 at 4:39 AM — ReplyHi Madam,
the way of tutor is awesome for Beginners.Thanks a lot
-
JAVAID IQBAL
June 8, 2015 at 4:40 AM — ReplyHi Madam,
Awesome for Beginners.
Thanks a lot
-
PNRao
June 8, 2015 at 9:39 AM — ReplyVery nice feedback! Thanks Javid!
Thanks-PNRao! -
Ekram
July 31, 2015 at 2:20 AM — ReplyI have been trying to learn Macro for sometime. And this is the first site that has been very helpful. I would definitely recommend this site for anyone to understand what to command and how. We need more ppl like you. Thank you for being such a awesome person.
-
Yogesh Kumar
August 30, 2015 at 6:53 PM — ReplyHi,
I am wondering for a VBA code to read entire data from active excel sheet and print line by line in immediate window.
I searched a lot but unfortunately I did not get any code. Can you help me ? Thanks in advance…Yogi
-
PNRao
August 30, 2015 at 11:31 PM — ReplyHi Yogesh,
You can loop through each cell in the used range of a worksheet and print in the immediate window, the below code will check the each cell, and it will print if the value is not blank.Sub sbReadEachCellPrintinImmediateWindow() For Each cell In ActiveSheet.UsedRange.Cells If Trim(cell.Value) <> " Then Debug.Print cell.Value Next End Sub
Hope this helps!
Thanks-PNRao! -
Yogesh Kumar
September 1, 2015 at 12:00 PM — ReplyHi PNRao,
Thanks a lot for your help, this code worked. May I know , how to print excel row data line by line ?
I want to print a row data in one line and next row data should be print in next line in immediate window or Is there any way to print entire excel sheet data in tabular form in immediate window. Please let me know if this is possible. Thank you in advance.Thanks
Yogi -
Yogesh Kumar
September 2, 2015 at 11:56 AM — ReplySub show()
Dim Arr() As Variant
Arr = Range(“A1:I12”)
Dim R As Long
Dim C As Long
For R = 1 To UBound(Arr, 1)
For C = 1 To UBound(Arr, 2)
Debug.Print Arr(R, C)
Next C
Next REnd Sub
This code prints a range as column in immediate window. Can any one tell me how to print data line by line ? I want to print one row data in one line and next data from next row should be print in next line in immediate window. Please help. Thanks in advance.
Yogi
-
PNRao
September 2, 2015 at 11:12 PM — ReplyHi Yogesh,
You need a small change in your code, see the below code to print each row in one line. In this example, we are storing all the data in a variable and printing for each record:Sub show() Dim Arr() As Variant Arr = Range("A1:I12") Dim R As Long Dim C As Long For R = 1 To UBound(Arr, 1) strnewRow = " For C = 1 To UBound(Arr, 2) strnewRow = strnewRow & " " & Arr(R, C) Next C Debug.Print strnewRow Next R End Sub
Thanks-PNRao!
-
Yogesh Kumar
September 3, 2015 at 1:31 PM — ReplyThank you very much PNRao. This code prints data exactly as per my need.
You are genius ! Hats off to you. Thanks a lot for your help. -
PNRao
September 3, 2015 at 3:59 PM — ReplyYou are most welcome Yogesh! I am glad you found this useful.
Thanks-PNRao! -
Yogesh Kumar
September 6, 2015 at 2:30 PM — ReplyHi
Sub show()
Dim Arr() As Variant
Arr = Range(“A1:I12″)
Dim R As Long
Dim C As Long
For R = 1 To UBound(Arr, 1)
strnewRow = ”
For C = 1 To UBound(Arr, 2)
strnewRow = strnewRow & ” ” & Arr(R, C)
Next C
Debug.Print strnewRowNext R
End SubIn this code I have to do some modifications that this code can read only even columns. Please help me.
Thanks in advance. -
driqbal
October 16, 2015 at 11:31 PM — ReplyYogesh Kumar September 6, 2015 at 2:30 PM
Reply
you need very little modification
Sub show()Dim Arr() As Variant
Arr = Range(“A1:I12”)
Dim R As Long
Dim C As Long
For R = 1 To UBound(Arr, 1)
strnewRow = “”
For C = 2 To UBound(Arr, 2) step 2
strnewRow = strnewRow & ” ” & Arr(R, C)
Next C
Debug.Print strnewRowNext R
End SubThis code will read only even columns.
-
driqbal
October 16, 2015 at 11:33 PM — ReplySub show()
Dim Arr() As Variant
Arr = Range(“A1:I12”)
Dim R As Long
Dim C As Long
For R = 1 To UBound(Arr, 1)
strnewRow = “”
For C = 1 To UBound(Arr, 2)
strnewRow = strnewRow & ” ” & Arr(R, C)
Next C
Debug.Print strnewRowNext R
End Sub -
Jon
November 6, 2015 at 3:26 PM — ReplyI am a V basic user of excel but have created a time sheet at work, i would like to take the information from these sheets completed by multiple people and bring the m together on one sheet is this possible, is it reasonably simple?
Any help would be great.
-
PNRao
November 7, 2015 at 11:21 AM — ReplyHi Jon,
We can use VBA to combine the data from different sheets and workbooks. It can be done in couple of hours based on the requirement. Please let us know if you want us to do this for you.Thanks-PNRao!
-
Abhishek
November 9, 2015 at 10:44 AM — ReplyHi,
I need one big help . Basically, i have 3 worksheets wherein one is the main template ,other one is mapping sheet and third one is the actual scattered data . Now i have to create a macro and run the same for multiple sheets with multiple formats. So, how we can write the vba code to read the scattered data from the 3rd worksheet and through mapping worksheet we can have the data entered into the main template . As the header available in in main template for eg . description can be different like description-value in the other worksheet having scattered data . So we have to use the mapping sheet to get the correct value .Kindly help
-
PNRao
November 9, 2015 at 3:39 PM — ReplyHi Abhishek,
We can use VBA to read the scattered data from the defined worksheet. It will take 2-3 hours based on the requirement. Please let us know if you want us to do this for you.Thanks-PNRao!
-
David
January 13, 2016 at 11:45 PM — ReplyI have a set of data A1:L171 that needs to be merged to multiple Excel templates. However, I only need B,F,G,H (Client, Date, Time AM, Time PM). So basically what I am looking for is a mail merge but through Excel. Is this possible?
-
Surendra
March 14, 2016 at 9:23 PM — ReplyI want to create a function which can help me to find if any particular cell has special character or not. can anyone help me out. thanks
-
Desk Tyrant
June 24, 2016 at 11:36 AM — ReplyHey,
thanks for the super quick tutorial. This stuff is pretty complicated at times and it’s hard to find solutions on the internet, but this helped me a bit so thanks again.
-
Claire
July 7, 2016 at 9:35 AM — ReplyHi,
I am trying to change the output from a macro that I have designed which is a series of combo boxes – currently the output is across the sheet, with each combo box selection being input into a consecutive column. I’d like them to be input into consecutive rows (so the text appears in a single column).
The current script that I’m using is:Private Sub CommandButton1_Click()
If Me.ComboBox1.Value = “Select” Then
MsgBox “Please select a wildlife health option”, vbExclamation, “Wildlife Health”
Me.ComboBox1.SetFocus
Exit Sub
End If
If Me.ComboBox2.Value = “Select” Then
MsgBox “Please select an option for normal ecology”, vbExclamation, “Normal ecology”
Me.ComboBox2.SetFocus
Exit Sub
End If
If Me.ComboBox3.Value = “Select” Then
MsgBox “Please select an option for disease”, vbExclamation, “Disease”
Me.ComboBox3.SetFocus
Exit Sub
End IfRowCount = Sheets(“Sheet2”).Range(“A1”).CurrentRegion.Rows.Count
With Sheets(“Sheet2”).Range(“A1”)
.Offset(RowCount, 1).Value = Me.ComboBox1.Value
.Offset(RowCount, 2).Value = Me.ComboBox2.Value
.Offset(RowCount, 3).Value = Me.ComboBox3.Value
End With
End SubAny help appreciated.
Regards,
Claire -
Ratish
September 29, 2016 at 7:27 PM — ReplyHi,
I am new to excel, My requirement is if I click a button on excel it should update the highlighted cell with the name of the person who clicked and with the time stamp
-
Man
October 5, 2016 at 12:41 PM — Reply -
Ram
July 8, 2017 at 3:23 PM — ReplyHi, Can some one help me on below request.
i have created excel sheet with data from columns A to E (inputs: column A,B,C,D and output: E)
how to read output (E column) data if i give input data from column A- D though macros.
-
PNRao
July 17, 2017 at 1:56 PM — ReplyExample:
Range("E1")=Range("A1")+Range("B1")+Range("C1")+Range("D1") If Range("E1")>70 Then MsgBox "Good" Else MsgBox "Try Again" End If
Hope this helps!
-
Zach
October 19, 2019 at 12:37 AM — ReplyHello,
Thanks for the cell tip, I was only aware of using Range(“A1”) = “text” to print to excel. Is there a way to print text to excel starting at a specified cell but not stating every other following text cells location.
Effectively Manage Your
Projects and Resources
ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.
We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.
Project Management
Excel VBA
Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.
Page load link
3 Realtime VBA Projects
with Source Code!
Go to Top
In this Excel tutorial lesson, I will guide you through the basics of Excel VBA and how you can use Excel VBA to write to a cell in an Excel sheet.
Recording a macro
Let’s start by recording macro. Go to the Ribbon to the Developer tab. Click the Record Marco button. Then click Edit. It will open the recorded macro as macro1 already created:
Now under this module, you can learn how to write any information to a cell say cell A1 of sheet1. Here are following way you can update value in cell:
Writing to a cell using worksheet
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = "hello"
Here «Sheet1» is the name of the sheet that you have used.
Run this macro:
Writing to a cell using index number of sheet
ThisWorkbook.Sheets(1).Range("A1").Value = "hello"
Here you are referencing the sheet using the index, which is 1 for sheet1. You can also check the total count of sheets in a workbook using sheets.count
Writing to a cell using code name
Sheet1.Range("A1").Value = "hello"
You are directly using sheet1 which is the sheet name here.
Writing to a cell using cells
ThisWorkbook.Sheets(1).Cells(1, 1).Value = "hello"
Instead of the range, you are using cells (row no, column no) where row no = 1 for the first row, column no = 1 for column A.
Writing a formula to a cell
Range("A6").Formula = "=SUM(A3:A5)"
This code writes the formula =SUM(A3:A5)
to cell A6 in the active worksheet. The formula will calculate the sum of the values in cells A3 to A5.
Further reading: Dynamic chart title from the cell How to hide the content of a cell in Excel? How to reference a cell?
I’ve been working with SQL and Excel Macros, but I don’t know how to add text to a cell.
I wish to add the text "01/01/13 00:00"
to cell A1
. I can’t just write it in the cell because the macro clears the contents of the sheet first and adds the information afterwards.
How do I do that in VBA?
asked Dec 16, 2013 at 13:43
2
Range("$A$1").Value = "'01/01/13 00:00"
will do it.
Note the single quote; this will defeat automatic conversion to a number type. But is that what you really want? An alternative would be to format the cell to take a date-time value. Then drop the single quote from the string.
answered Dec 16, 2013 at 13:44
BathshebaBathsheba
231k33 gold badges359 silver badges477 bronze badges
3
You could do
[A1].Value = "'O1/01/13 00:00"
if you really mean to add it as text (note the apostrophe as the first character).
The [A1].Value
is VBA shorthand for Range("A1").Value
.
If you want to enter a date, you could instead do (edited order with thanks to @SiddharthRout):
[A1].NumberFormat = "mm/dd/yyyy hh:mm;@"
[A1].Value = DateValue("01/01/2013 00:00")
answered Dec 16, 2013 at 13:47
FlorisFloris
45.7k6 gold badges70 silver badges122 bronze badges
7
You need to use Range
and Valu
e functions.
Range
would be the cell where you want the text you want
Value
would be the text that you want in that Cell
Range("A1").Value="whatever text"
answered Mar 7, 2016 at 10:21
GarryGarry
611 silver badge1 bronze badge
You can also use the cell property.
Cells(1, 1).Value = "Hey, what's up?"
Make sure to use a .
before Cells(1,1).Value
as in .Cells(1,1).Value
, if you are using it within With
function. If you are selecting some sheet.
enamoria
8762 gold badges11 silver badges29 bronze badges
answered Dec 19, 2018 at 6:40
Setting up the module
Open the Visual Basic Editor (VBE) by using the shortcut key ALT + F11.
Right click in the Project Explorer window and select Insert Module.
The module can be renamed in the Properties Window.
In this example, the name LessonsRanges is used.
Writing the Sub procedures
Options Explicit is important when the Sub procedures work with variables.
Create a new Sub procedure by starting with the keyword Sub.
Pressing ENTER will automatically add the End Sub statement.
The methods for a particular Sub procedure should be written in the space between these.
The Immediate Window is useful when doing tests.
To activate this, click View > Immediate Window or by using the shortcut key CTRL + G on the VBE.
Note that when the worksheet name is not indicated in the code, it automatically executes the statements in the active sheet.
Active cell vs Selection
ActiveCell is the cell where the cursor is.
Selection refers to the cell or range that is highlighted.
This can be tested out by highlighting a range on the Sheet and writing the following statements in the Immediate Window:
?ActiveCell.Address?Selection.Address
Referencing a cell/range and changing the value
Assigning a value of a cell or range can be done using statements in the sub procedure.
There are various ways to refer to cells and ranges using a combination of punctuation marks.
The general syntax of referring to cells and ranges is Range(Cell1, [Cell2]).
Values are then assigned by using the Value property (.Value) or by using the = symbol.
NOTE: Adding Cells.Clear at the start of the subprocedure ensures that all cells are emptied before the methods are executed.
Below are the different ways to refer to ranges.
To test the sub procedures, press the F5 button.
- Single cell
Range(“A1”).Value = ”1st”
- Single range using a colon
Range(“A2:C2”).Value = “2nd”
- Multiple ranges separated by a comma
Range(“A3:C3,E3:F3”).Value = “3rd”
- Multiple cells separated by a quotation marks and a comma
Range(“A4,C4”).Value = “4th”
- Single range by specifying the start cell and end cell
Range(“A4”,”C4”) = “5th”
- Single range by concatenating the column letter and row number. This is useful when using a loop with a variable in place of the row number.
Range(“A” & 6,”C” & 6) = “6th”
- Using the Cells property of the Range object by specifying the row number and column number. This is especially useful when looping through many columns and different rows.
Range(Cells(6,1), Cells(6,3)).Value = “6th”
- Highlighting a range and referencing a specific cell within that range.
Range(“A4:C7”).Cells(4,2)).Value = “7th”
OFFSET()
This function allows you to change a value of a cell by specifying a starting point and the number of rows and columns to offset from it. This is done using the Offset property of the range.
Syntax is as follows: Offset(number of rows, number of columns)
- Offset a cell
Range(“A1”).Offset(7,2)).Value = “8th”
- Offset a range
Range(“A1”).Offset(7,2)).Range(“A1:A4”).Value = “8th”
Range(“A1:B1”).Offset(8,1)).Value = “9th”
Using the name manager
Rename the cell by selecting a cell and going to the name box.
After which, a value can be assigned to this specific cell:
Range(“LastOne”). Value = “10th”
Going through each line of code (Debug)
To do this, click anywhere in the Code Window and press F8. It will then highlight a single row and executes it as you scroll past it.
To resume and run through the rest of the code, press F5 or play.
Another way to do this is to go to Debug > Step Intro.
Referencing entire rows and columns
This is similar to referencing a range.
In the examples below, the RowHeight and ColumnWidth properties will be adjusted.
- Refer to rows and specify a row height
Rows(“12:14”).RowHeight = 30
- Refer to separate rows
Rows(“16:16,18:18,20:20”).RowHeight = 30
- Refer to columns
Columns(“E:F”).ColumnWidth = 10
- Refer to separate columns
Range(“H:H,J:J”).ColumnWidth = 10
- This adjusts the width of columns H and J, skipping column I.
Range(Columns(1),Columns(3)).ColumnWidth = 5
This adjusts the first column to the third column
Autofit can also be done by using Cells.Columns.AutoFit.
Summary
Published on: April 12, 2018
Last modified: March 17, 2023
Leila Gharani
I’m a 5x Microsoft MVP with over 15 years of experience implementing and professionals on Management Information Systems of different sizes and nature.
My background is Masters in Economics, Economist, Consultant, Oracle HFM Accounting Systems Expert, SAP BW Project Manager. My passion is teaching, experimenting and sharing. I am also addicted to learning and enjoy taking online courses on a variety of topics.
Write to cell and change worksheet values with VBA (and VBA object hierarchy)
Writing values in your Excel sheets with VBA can sometimes be a bit
tricky. But it is essential for a correct understanding of VBA and the
interaction between the user and the sheet. This interaction is done via
OBJECTS.
This Objects are like grand-parents, parents and
children . There is an order to all this.
The Object on the top, the common ancester, the great great great grand
father or mother is Excel itself. It is called Application.
Then comes the Workbooks: this is the name of your file
like «Myfile.xlsx» or «Myfile.xlsm» where m stands for macro.
Then comes the Worksheets which are the different sheets
you have in your Workbook.
Then comes the Range which defines the cells that we are interested in.
And then comes the Attribute to the cell.
In VBA you can write a value in cell like in that case we
give the cell A1 the value of «excel made easy».
Application.Workbooks(«Book1.xlsm»).Worksheets(«Sheet1»).Range(«A1»).Value
= «excel made easy»
This was it for a short introduction to hierarchies of Objects in Excel.
Now lets look at this in more details and how to write to a cell with VBA.
To write a variable to a cell from VBA, you have to know in which worksheet the cell
is located. Is it the sheet1 or sheet2, ….?
So to enter a value in a cell use following command line.
Worksheets(1).Range(«A1»).Value = «Hello»
Where Worksheet(1) refers to the first worksheet. You can see, this is
referred with a number 1, before we used its name «Sheet 1».
Here an example on how to write in the sheet
in different manners using VBA.
You can see that there are many ways to write text in a sheet.
Worksheets(1).Range(«A1»).Value = «Hello»
Worksheets(1).Range(«A2»).Value = «Lets»
Worksheets(«FirstSheet»).Range(«A3»).Value = «write»
Worksheets(1).Range(«A4»).Value = «some»
Sheet1.Range(«A5»).Value =
«text»
TIP:
It is important to understand the principle of hierarchy in Excel or any
MS Office application.
In the following line, we write in the sheet1, in cell A6 the
value BYE.
Application.Workbooks(«write_in_sheets.xlsm»).Worksheets(1).Range(«A6»).Value =
«BYE»
To write a variable to a cell in Excel with VBA, write following VBA code.
Application.Workbooks(«write_in_sheets.xlsm»).Worksheets(1).Range(«A6»).Value =
MyVariable
Where MyVariable contains the value you want to write to the Cell
Application is the top hierarchical level.
Workbook(name of the file) will specify which file you are working in.
The next part Worksheet(1),Range() are explained before.
Add a workbook with VBA
This will create a new workbook.
Workbooks.Add
Add a worksheet in Excel with VBA
This will add a worksheet in you workbook.
Worksheets.Add
Count the worksheets in VBA
This will return the number of worksheet in you workbook.
MsgBox Worksheets.Count will display the
number of worksheets.
a= Worksheets.Count will give to the
value a the number of worksheets.
Workbooks.Count will count the number of workbooks
the Count property of the Workbooks collection counts the number of active
workbooks.
Please Tweet, Like or Share us if you enjoyed.