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

Changing Table Cell Text Direction

When creating a table, you can turn the orientation of the text, within a cell, by ninety degrees in either direction ...

Discover More

Deleting a Hyperlink

Hyperlinks can be helpful in some worksheets but bothersome in others. Here's how to get rid of any hyperlinks you don't ...

Discover More

Selecting to the Next Punctuation Mark

Writing macros often involves selecting different parts of your document so that some sort of processing can be ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Relative VBA Selections

Need to select a cell using a macro? Need that selection to be relative to the cell you currently have selected? Here's ...

Discover More

Digital Signatures for Macros

The security features built into Excel allow you to digitally sign your macros so that users can rest assured that they ...

Discover More

Documenting Changes in VBA Code

Your company may be regulated by requirements that it document any changes to the macros in an Excel worksheet. Your ...

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 nine minus 4?

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.