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

Switching Headers in a Frozen Row

Excel allows you to "freeze" rows in your worksheet. What if you want the rows that are frozen to change as you scroll ...

Discover More

Counting Precedents and Dependents

Do you need to know how many precedents or dependents there are on a worksheet? You could count them manually, or you could ...

Discover More

Getting Rid of Hidden Text in Many Files

Hidden text is a great boon if you want to make sure something doesn't show up on the screen or on a printout. If you want to ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Easily Changing the Default Drive and Directory

Need a quick way to change the default drive and directory in a macro you are writing? Here's the commands to do it and a ...

Discover More

Comparing Strings

As your macro is processing information, there will doubtless be times that it will need to compare information in strings. ...

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
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 one more than 9?

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.