OriginLab Corporation - Data Analysis and Graphing Software - 2D graphs, 3D graphs, Contour Plots, Statistical Charts, Data Exploration, Statistics, Curve Fitting, Signal Processing, and Peak Analysis     
 
Skip Navigation Links
All BooksExpand All Books
Quick HelpExpand Quick Help
ProgrammingExpand Programming

4.8 FAQ-367 How do I delete every nth row or column from my worksheet?

Last Update: 8/27/2015

Delete every nth row or column by X-Function

To delete columns, use the wreducecols X-Function.

For example, the script below will delete every 5th column in the active workbook (delete 1 column and skip 4 columns, start from the 5th column):

wreducecols skip:=4 start:=5;

To delete rows, use the wreducerows X-Function.

For example, the script below will delete every 4th row in the active workbook (delete 1 column and skip 3 columns, start from the 4th column):

wreducerows nrows:=1 skip:=3 start:=4;


Delete every nth row or column by looping

With your worksheet active, open the Script Window (menu item: Window: Script Window), copy-paste the following lines of code, select all lines, and press Enter.

To delete columns:

// Delete every nth column, counting from the left
int ndel = 3; // change this number as needed;

int ncols = wks.ncols;
int nlast = ncols - mod(ncols, ndel);

// Start deleting from the right to the left
for(int ii = nlast; ii > 0; ii -= ndel)
{
   delete wcol(ii);
}

To delete rows:

// Delete every nth row, couting from the top
int ndel = 3; // change this number as needed;

int nrows = wks.nrows;
int nlast = nrows - mod(nrows, ndel);

// Start deleting from the bottom to the top
for(int ii = nlast; ii > 0; ii -= ndel)
{
  wks.DeleteRows(ii);
}

Delete every nth row using the Worksheet Query dialog

  1. With the worksheet active, open the Worksheet Query dialog by clicking Worksheet: Worksheet Query from main menu.
  2. Enter mod(i,3)==0 (refer to mod function) in the Condition box, which means select the every 3rd row. Then click the Test -- select if True button to highlight these rows.
  3. Close this dialog and delete these rows from worksheet by selecting Delete from right-click menu.



Keywords:remove, reduce

Minimum Origin Version Required: 2016 SR0

 

© OriginLab Corporation. All rights reserved.