Find Last Row of a Specific Column

How to find the last row of a specific column, rather than the entire sheet.

Important notes:
* Make sure the variable used to store the row is declared and instantiated outside and above the FOR() loop so that it can be accessed and used outside and below the FOR() loop. Otherwise you have to declare another variable just to hold the ending FOR() loop value.
* Make sure the row value is incremented once more before using it as the target sheet range, or you will override the current last row of data rather than adding a new row of data.

Sheet (to copy):
https://docs.google.com/spreadsheets/d/1eoFXjznj5p8LwUr9-O3z5HHhuxd7fpZ_vtMcUlmqrH8/edit?copiedFromTrash#gid=741712631

Script:
function onEdit(e) {
var s = e.source.getActiveSheet();
if(s.getName() != “Purchases” || e.range.columnStart != 6 || e.value != “X”) return;
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Cleared”);

var cData = targetSheet.getRange(1,1,targetSheet.getLastRow()).getValues();
var c = 0;
for (c; c < cData.length; c++){
if (cData[c][0] == “”)
break;
}
c++;

if (c == targetSheet.getMaxRows())
targetSheet.insertRowsAfter(c,20);

var r = e.range.rowStart;
s.getRange(r,1,1,5).copyTo(targetSheet.getRange(c,1,1,5), {contentsOnly:true});
s.deleteRow(r);
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *