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)

8

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

Including Datasheet Rows or Columns in a Chart

Controlling which datasheet information is graphed in a chart.

Discover More

Creating an Inline Heading

When settling on an overall design for your document, you need to decide how you want your headings to appear. If you want ...

Discover More

Formatting Captions

When you add captions to elements in your document, Word allows you to modify how those captions are formatted. Here's how ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

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

Updating Automatically When Opening Under Macro Control

If your workbook contains links, you are normally given the opportunity to update those links when you open the workbook. ...

Discover More

Running a Macro When a Worksheet is Deactivated

You can easily configure Excel so that it runs a specific macro whenever a worksheet is deactivated. Just follow the easy ...

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 + 1?

2017-05-31 11:56:58

John Hudson

I have a Budget established in Excel 2007 with a column to input bill amounts (totaled at the bottom). There is an item due bi-annually for the same amount each time. I wrote the following formula: =IF(A35=1,"Pay Now", IF(A35=6,"Pay Now",)) which is placed in the adjacent cell as a "pay" notification. Note that I convert a date formula to the date month numerical (example: Cell A35 May = 6) The result of this is to show the "Pay Now" reminder on Jan 1 and again on Jun 1. I would like to write a macro that would automatically enter the "numerical amount due" ($498 for example) into its space in that column on Jan 1 and Jun 1 when I am prompted by the "Pay Now" message. Back in the day I was pretty handy with macros, but at +70 years of age I've lost a lot of daily experience. Any assistance would be greatly appreciated. John


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.