Excel: Advantages and Uses of the OFFSET FunctionLast reviewed: November 30, 1994Article ID: Q63962 |
The information in this article applies to:
SUMMARYThere are two basic methods of calculating a reference relative to another reference in a Microsoft Excel macro.
BENEFITS OF USING THE OFFSET FUNCTION
StructureOFFSET is a function with a structured syntax. Syntax errors are automatically recognized by Microsoft Excel. The methods are controlled by the function syntax.
ClarityWith OFFSET, relative R1C1 text notation can be entirely avoided. This can eliminate confusion associated with R1C1-style references if A1-style references are preferred. Also, text references (such as R1C1) evaluate at run time, whereas reference values (such as A1 and OFFSET) evaluate when they are entered into a macro sheet.
SpeedComplicated text reference notation takes longer to evaluate to a reference value than does the OFFSET function.
SimplicityThe same operations take fewer steps with OFFSET than with R1C1 references as text.
MORE INFORMATIONThe syntax of OFFSET is as follows:
=OFFSET(ref,rows,cols,height,width)OFFSET is a value-returning macro function that returns a reference value offset by "rows" and "cols" from the reference value "ref." The optional arguments "height" and "width" are used to calculate a reference that is in a different shape than "ref." You cannot display a reference value in a cell. When values are displayed on a macro sheet, the contents of the upper-left cell of the reference returned by OFFSET will be displayed. However, it is important to understand that the reference value desired is returned implicitly by the OFFSET function. You may want to show the reference value returned by OFFSET when testing the macro. Following are two convenient methods to show the reference value:
Method 1Step through the formula in the Single Step dialog box:
Method 2Use REFTEXT to display the reference as text:
Below is a comparison between specifying a relative reference with text notation and using OFFSET. Although text value notation is shorter in certain special cases, it usually takes slightly longer to evaluate and is certainly less structured. Note the structure and repetition of a few basic techniques when using OFFSET. The format for these examples is as follows: #. Desired result:
Text Value Notation Reference Value Notation "r[1]c" OFFSET(ACTIVE.CELL(),1,0) "rc:r[3]c[2]" ACTIVE.CELL():OFFSET(ACTIVE.CELL(),3,2) "rc[5]:r["&ROWS(SELECTION())-1&"]c["&COLUMNS(SELECTION())-1+5&"]" OFFSET(SELECTION(),0,5) number of rows desired and the number of columns desired, respectively): "r["&numofrows&"]c["&numofcols&"]" OFFSET(ACTIVE.CELL(),numofrows,numofcols) Also note that the text value notation for the third and fourth examples would be even more unwieldy if they didn't take advantage of the ability of Microsoft Excel to coerce number values into text values during string concatenation. For more information on using OFFSET, STEP, and REFTEXT with Excel for the Macintosh, see pages 163-164, 227, and 192-193, respectively, in the "Microsoft Excel Function Reference" version 3.0 manual. If you are using Excel 2.2, see pages 251-252, 286, and 266, respectively, in the "Microsoft Excel Functions and Macros" version 2.2 manual.
|
KBCategory:
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |