Home > Enterprise >  How to move row from one sheet to another based on value in Column P
How to move row from one sheet to another based on value in Column P

Time:04-20

I have a spreadsheet with a header. In column P, I have a column of percentages. Anything over 150%, I want to move that entire row over to a new worksheet called "Groups".

I don't have much code right now because I tried about 5 different ways and wound up deleting my work. :/

CodePudding user response:

If you want to copy the information from the columns to another spreadsheet you can try this:

function copyToSheet() {
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var rows = sourceSheet.getDataRange().getValues();
  var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
  for(var i=0; i< rows.length; i  )
  {
    if(rows[i][2]>150)
    {
      targetSheet.appendRow(rows[i]);
    }
  }
}

The script takes the information from the source sheet and then compares the data from column C for each row and if the value is higher than 150 it copies the whole row to a different sheet.

This is my sample data in Sheet1:

enter image description here

Here is the result in Sheet2 after running the script:

enter image description here

You can adapt the script depending of your needs.

CodePudding user response:

If you have some AppScript experience, this may help:

  1. sort range by percent column descending;

  2. find first row with percent below 150%;

  3. Get values from range rows 2 to that row minus 1;

  4. Delete rows 2 through that row minus 1;

  5. set values to other tab with range starting at getLastRow()

  • Related