Store row number of active cell in variable, switch to another sheet, put stored number in cell I3, end macro

641 Views Asked by At

Trying to write a simple macro in Google Sheets, simple stuff but I'm not familiar with the language; extremely frustrated. Please help.

I have two active worksheets in my workbook, Hotrods is a row delimited list, CarDetail shows the details of the selected row... currently I input the row number of Hotrods in Cardetail to fill the form.

I'm trying to write a macro that stores the current row of Hotrods in a variable... switches to CarDetail, puts the stored value from Hotrods in Cell (I,3), then terminates with the focus on Cell (c,4) of Cardetail. The VBA editor keeps throwing errors. I'm missing something obvious!

Please point me towards some good reading or give me some good tips! Thanks

1

There are 1 best solutions below

4
On

I got it working... probably not the cleanest code but it does the job!

function CarDetailForm() {                                      ;
  var fromSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  // Should only run from the HotRods sheet 
  if (fromSheet = "HotRods") {                          ;
    var s       = SpreadsheetApp.getActive()             ;
    var ss      = SpreadsheetApp.getActiveSheet()        ;
    // Store HotRods active row number
    var rownum  = ss.getCurrentCell().getRow()           ;
    // Activate the CarDetail sheet
    s.setActiveSheet(s.getSheetByName('CarDetail'), true)  ;
    // activate cell I3 - or it won't work
    s.getRange('I3').activate()                          ;
    // set I3 to the row number carried from HotRods
    s.getCurrentCell().setValue(rownum)                  ;
    // exit macro with cursor in date field of CarDetail
    s.getRange('C4:I4').activate()                       ;
  }                                                      ;
}                                                        ;