Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Inserting Worksheet Values with a Macro.

Inserting Worksheet Values with a Macro

by Allen Wyatt
(last updated February 17, 2016)

6

Inserting values into a cell is done quite often in macros. In order to insert information into a cell, you use the Value property. For instance, you could use the following to insert a number (23) into cell A1:

Cells(1, 1).Value = 23

For entering information in a cell, the Value property is applicable to any object that resolves to a range. Thus, you could use the following to place a text value ("Address") into the cell at C4:

Range("C4").Value = "Address"

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12613) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Inserting Worksheet Values with a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Creating a Hyperlink to a Specific Page

Most people add hyperlinks in a document to reference pages on the Web. You can, however, create hyperlinks to other Word ...

Discover More

Quickly Changing Font Sizes

A quick little shortcut can help you easily step through different font sizes for whatever text you've selected. Word ...

Discover More

Filtering for Comments

Excel makes it easy to filter a data table based on various values in that table. It isn't so easy to filter according to ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Creating a Plus/Minus Button

Want a quick way to convert positive values to negative and vice versa? You can create your own plus/minus button by using a ...

Discover More

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can limit ...

Discover More

Understanding the While...Wend Structure

Logical structures are important in programming, as they allow you to control how the programming statements are executed. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 2?

2016-02-26 06:59:26

Barry

@Lynn,

The code you specify changes the selection which may or may not be required/desirable. But you can achieve the same result in single line of code without changing the selection, and so is faster and more efficient.

cur_month1= range("cur_month") 'to get the value stored in this cell

OR

range("Cur_month")= 1234 'to set the value (change 1234 for whatever value you want or . use a variable)


2016-02-25 17:11:28

Lynn Martin

This is good for entering or getting values for a named range:

Application.GoTo Reference:="cur_month"
cur_month1 = ActiveCell.Value

This goes to the named range CUR_MONTH, then puts that value into a variable in your macro called CUR_MONTH1.


2016-02-17 15:36:15

Thomas Papavasiliou

The ".Value" is not absolutely necessary.

Cells(1,1)=23

works as well.


2013-05-06 11:30:10

Don

Building on the tip for Named ranges, my recurring report projects have a worksheet named "Controls". In there I build a table that has labels in column A, and the values go in column B, then name the ranges with the Create from Selection command in the UI.

Macros use statements like the following to keep them current, then the values are used to control what people may do and validating inputs
Workbook.Names

For example, when starting a month's report, the following establishes the scope of the report for a specific month.

wkbkReport.Names.("Dept_Code").RefersToRange = frm1.DepartmentProfile.Department
wkbkReport.Names.("Reporting_Month").RefersToRange = frm1.DepartmentProfile.Month

Then as the daily input files are validated, a function can validate whether it should be included

Const InputDeptCodeLocation as String = "B2"
Const InputReportDateLocation as String = "B3"

If wkbkInput.Sheets(1).Range(InputDeptCodeLocation ) = wkbkReport.Names("Dept_Code").RefersToRange And Month(wkbkReport.Sheets(1).Range(InputReportDateLocation)) = wkbkReport.Names.("Reporting_Month").RefersToRange Then ...


2013-05-06 08:14:04

Bryan

Or my personal favorite:

Range("Address") = var

where var is a 2-dimensional, base-1 variant array the same size and shape as Range("Address")


2013-05-04 05:16:03

Barry Fitzpatrick

The above will put these values into cells on the Activesheet. By prefixing the Cells or Range method with the worksheet name or index you can put a value in any worksheet with first having to activate it.

e.g. Worksheets("Sheet1").Cells(1, 1).Value = 23
or Worksheets(1).Range("C4").Value = "Address"


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.