
' Public Sub UsingCodeName() ' Write number to cell A1 in sheet1 of this workbook Sheet1 instead of ThisWorkbook.Worksheets(“Sheet1”). The following code shows the above example using the code name of the worksheet i.e. To do something with Range you must first specify the workbook and worksheet it belongs to.įor the rest of this post I will use the code name to reference the worksheet. This follows the same hierarchy as in Excel so should be easy to understand. ThisWorkbook.Worksheets( "Sheet1").Range( "A3").Value2 = # End SubĪs you can see Range is a member of the worksheet which in turn is a member of the Workbook. ThisWorkbook.Worksheets( "Sheet1").Range( "A2").Value2 = "John Smith" ' Write date to cell A3 in sheet1 of this workbook ThisWorkbook.Worksheets( "Sheet1").Range( "A1").Value2 = 67 ' Write text to cell A2 in sheet1 of this workbook ' Public Sub WriteToCell() ' Write number to cell A1 in sheet1 of this workbook
.resize excel vba how to#
The following example shows you how to place a value in a cell using the Range property. The Range property takes the same argument that most Excel Worksheet functions take e.g. The worksheet has a Range property which you can use to access cells in VBA. It is better to use Value2 as it will always return the actual cell value(see this article from Charle Williams.) If you don’t use any property then the default is Value. Using Value may truncate number if the cell is formatted as currency. You may be wondering what is the difference between Value, Value2 and the default: ' Value2 I have recently updated this article so that is uses Value2. Let’s start with the simplest method of accessing cells – using the Range property of the worksheet. In this post I will tackle each one, explain why you need it and when you should use it. Generally speaking, you do three main things with CellsĮxcel has a number of methods for accessing cells such as Range, Cells and Offset.These can cause confusion as they do similar things and can lead to confusion

Almost everything you do in Excel starts and ends with Cells.

These three elements are the Workbooks, Worksheets and Ranges/Cells. Cells are by far the most important part of Excel. This is the third post dealing with the three main elements of VBA.
.resize excel vba full#
( Note: Website members have access to the full webinar archive.) If you are a member of the VBA Vault, then click on the image below to access the webinar and the associated source code.

13 Reading Values from one Cell to another.
