Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Inserting Worksheet Values with a Macro

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

Related Tips:

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!

 

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 5+3 (To prevent automated submissions and spam.)
 
 
           Commenting Terms

Comments for this tip:

Barry    19 Jan 2017, 06:03
@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.
Barry    19 Jan 2017, 05:36
@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
Theresa    18 Jan 2017, 10:44
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
Jayz    11 Jan 2017, 21:28
how if , I have data in worksheet, and I want to show my data using userform and macro. What would I do ?
Barry    31 Oct 2016, 06:39
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.
swattsup    30 Oct 2016, 16:53
I would be leery of getting tips from someone who puts up examples that do not work.

Worksheets.("Sheet3").Range("C4").Value = "Address"
gregor stewart    25 Oct 2015, 05:03
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"
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2017 Sharon Parq Associates, Inc.