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

Inserting Worksheet Values with a Macro

by Allen Wyatt
(last updated November 28, 2016)

7

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"

The above examples insert values directly into the currently active worksheet. You can enter values into a different worksheet by prefixing the examples with a worksheet object, in this manner:

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

If the worksheet you specify does not exist (it is not currently open in Excel), then the code generates an error. In addition, if the worksheet you are trying to modify is a chart or the cell is protected and the worksheet locked, then you also get an error.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2313) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Adjusting a Range's Starting Point

Select a range of cells, and one of those cells will always be the starting point for the range. This tip explains how to ...

Discover More

Selecting Text in Linked Text Boxes

Text boxes are often used as design elements in a document layout. If you have linked text boxes, you may have noticed that ...

Discover More

Sorting by Highlighting

The sorting capabilities of Word are quite handy and easy to use. What if you want to sort by something Word doesn't sort by, ...

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

Creating a String in a Macro

Need to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...

Discover More

Saving a Workbook in a Macro

Does your macro need to make sure that the workbook being processed is saved to disk? You can add the saving capability by ...

Discover More

Using Message Boxes

When creating a macro, one of the ways you can communicate with users is through the use of a message box. This tip explains ...

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 for this tip:

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. 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 9 - 7?

2017-01-19 06:03:17

Barry

@Theresa,

You do not need a macro to do this.

You could achieve this using a nested "IF" function in cells in column G. This can look very messy if you need to nest to a depth of more than 3 or 4 deep.

Starting in cell G2 the first level would be the formula:

=IF(E2=1,F2+182,<next level formula>)

the "<next level formula>" would take the form If(E2=""A",F2+14,<next level formula>)

I've assumed that column contains a date and column G has some kind of adjusted date, and that these columns are formatted as dates.(Note: I've made the assumption that 6 months is 182 days). The formula adds a specified number of days to the date in column F. At the deepest level the "<next level formula>" would be the formula in the situation where an invalid value in column E has been entered.

If the nesting is more than 2 or 3 deep then I would suggest creating a table of the column E codes and their corresponding date offset in days and use a VLOOKUP function. So let's say you've created this table and named it "Offsets" then the formula in cell G2 would be:

=F2+VLOOKUP(E2,Offsets,2)

This formula can then be copied down column G.


2017-01-19 05:36:32

Barry

@Jayz

This tip is primarily about using a macro to put data into a cell on a worksheet. but the reverse is just as easy lets say you have a userform called "Uferform1" and on that userform you have a text box called "TextBox1". Then the code to copy the value from the worksheet to the text box would be:

Userform1.TextBox1.Value = Sheet3.Range("A1").Value

If then the User changes the value in the textbox you could subsequently (say by clicking a "Submit" button) transfer (copy) the modified value back to the worksheet using the code:

Sheet3.Range("A1").Value = Userform1.TextBox1.Value


2017-01-18 10:44:25

Theresa

I am trying to get info from column e and column f to perform a calculation in column g. If e=1 then column f + six months = column g. If column e=2A then column f + two weeks = column g. If column e=2B then column f + one week = column g. Can you assist me please? Thank you. Theresa


2017-01-11 21:28:38

Jayz

how if , I have data in worksheet, and I want to show my data using userform and macro. What would I do ?


2016-10-31 06:39:43

Barry

The code specified (excluding the "." typo after "Worksheets") will work fine until someone changes the TAB name of the worksheet.

A better way would be to use the CODENAME of the worksheet like this:
Sheet3.Range("C4").Value = "Address" (assuming the codename of the required sheet is "Sheet3").It doesn't matter then what a user might change the TAB name to, or the order of the worksheets (another way of addressing a worksheet is using its index number which is actually the number within the order of worksheets starting from the left - first sheet is 1, second sheet is 2, etc. etc.).

Of course, there'll always be a problem if a user deletes a worksheet, and therefore the workbook's structure should have protection turned on to protect against this.


2016-10-30 16:53:15

swattsup

I would be leery of getting tips from someone who puts up examples that do not work.

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


2015-10-25 05:03:24

gregor stewart

I'm quite sure that the code to change a cell value in another sheet is not:

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

...Rather, it is without the . after "Worsksheets" i.e. Worksheets("Sheet3").Range("C4").Value = "Address"


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.

Links and Sharing