Microsoft KB Archive/103165

{|
 * width="100%"|

XL: Macro to Select a Range Every nth Row/Column

 * }

-

The information in this article applies to:


 * Microsoft Excel for Windows, versions 2.x, 3.0, 4.0, 4.0a
 * Microsoft Excel for OS/2, version 2.2, 3.0
 * Microsoft Excel for the Macintosh, versions 2.x, 3.0, 4.0

-

SUMMARY
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

The following macro selects a 10-column-wide range every third row. The dimensions of 10 and 3 can be changed to any numbers you want to use. You can also adjust this macro to select a 10-row-deep range every third column by transposing "Row_Col" with "0" in cell A8 below and changing "Height" to 10 and "Width" to 1.

To select two ranges and leave every third blank, change "Height" in A4 to 2.

Sample Macro
  A1:   =SET.NAME("Reference",!A1) A2:  =SET.NAME("End_Row_Col",100) A3:  =SET.NAME("Step",3) A4:  =SET.NAME("Height",1) A5:  =SET.NAME("Width",10) A6:  =SELECT(Reference) A7:  =FOR("Row_Col",0,End_Row_Col,Step) A8:  =SELECT((SELECTION,OFFSET(Reference,Row_Col,0,Height,Width))) A9:  =NEXT A10: =RETURN

Explanation of Macro
  A1: Names the starting cell "Reference."

A2: Defines the name "End_Row_Col" to be the number which, added to the row or column of "Reference", equals the absolute ending row or      column.

A3: "Step" is the "n" For Every nth row/column. The 3 can be       changed. For example, if you want to select a range of 10 columns every 4 rows, you would change the 3 to 4.

A4: Sets height when selecting columns.

A5: Sets width when selecting rows.

A6: Selects the range called "reference."

A7: Begins counting rows or columns.

A8: This statement adds what is selected by the OFFSET function to      what is already selected. Each FOR-NEXT loop adds another row or      column.

A9: Increments the FOR loop.

A10: Ends the macro.