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.
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!
Need to select a cell using a macro? Need that selection to be relative to the cell you currently have selected? Here's ...
Discover MoreThe security features built into Excel allow you to digitally sign your macros so that users can rest assured that they ...
Discover MoreYour company may be regulated by requirements that it document any changes to the macros in an Excel worksheet. Your ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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"
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2018 Sharon Parq Associates, Inc.
Comments