I am creating a macro for my repetitive work.
What I want to do:
- Copy a current cell in Sheet1
- Paste it to Sheet2
- There will be a =filter formula running at the bottom, and I need to navigate to the bottom and copy the range of outputs
- Have the cell return back to its original A1 cell position in Sheet2
- Paste the outputs to Sheet3, and move down one row
- Loop it until it meet an empty cell in the row of Sheet1
Challenges I face:
- In Sheet1: The macro I created only refer to the cell I first run the macro with (even after I start from a different cell, the macro still copying the same initial cell)
- In Sheet2: The outputs will possibly be a row or multiple rows of output, so it seems like Ctrl+A during the macro may not quite do the work.
- For Loop: The macro only me to run once, but I will need it run repetitively until it meets an empty cell in Sheet1.
Challenge 1 and 2 are my main challenges, I can manually use hotkeys to run the macro if I can't get the macro to loop, but will be definitely grateful if someone can teach me how to loop it.
function CleanUp6() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true);
spreadsheet.getRange('\'Sheet1\'!C2').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getActiveRange().getDataRegion().activate();
currentCell.activateAsCurrentCell();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet3'), true);
spreadsheet.getCurrentCell().offset(1, 0).activate();
spreadsheet.getRange('\'Sheet2\'!A8:D8').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
Using macro recorder to try to develop a script is not a good idea. It is recording mouse and keyboard actions, not code logic. I think what you want is something like this. Can't be sure but try it on a copy of your spreadsheet. Add a menu option with the onOpen() trigger.