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

ID: Q103165


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. 


REFERENCES

"Function Reference," version 4.0, pages 154-155, 299-230, 378-379, 383, 384, 389-390

"Function Reference," version 3.0, pages 79-80, 163-164, 209-210, 214, 217-218

Additional query words: 2.0 2.00 2.01 2.1 2.10 2.2 2.20 2.21 3.0 4.00a other nonadjacent non-adjacent discontiguous

Keywords : kbcode kbprg
Version : WINDOWS:2.0,3.0,4.0,4.0a; OS/2:2.2,3.0,MACINTOSH:2.0,3.0,4.0
Platform : MACINTOSH OS/2 WINDOWS
Issue type :


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.