Macro to add formula if cell in column equals X

qrtback10

New Member
Joined
Jan 19, 2015
Messages
20
Hey guys, I have the script below sort of working. It's posting the formula like it should if column 'K' = "PENDING" and column 'R' = "". No problems so far. The issue that I'm having is that when it posts the formulas, it posts the exact formula that's in the script. I've ran others (and mistakenly assumed that this would) where the formula starts at H2+G2 but if the criteria to match is in row 11, it would go to H11 + G11. In my case now it doesn't do that. I need it to. Any matches it finds, it puts the formula 'H2+G2' but I need the formula numbers to match the row number the function is ran in. Hopefully that makes sense.

This is what I currently have:

Code:
function fillColB() {  var s = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = SpreadsheetApp.getActive().getSheetByName('Locate');
  
  var data = sheet.getDataRange().getValues();
  var data_len = data.length;
  for(var i=0; i<data_len; i++) {
    if(data[i][17] == "") {
    if(data[i][10] == "PENDING") {
      var cell = sheet.getRange(i+1,12).setFormula('H2+G2');
      sheet.getRange(i+1,13).setFormula('=C2+M2');
      }
    }
  }
}

Any help greatly appreciated! Thanks.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
For some reason the full function doesn't show up above. Here's the full script:

function fillColB() {
var s = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActive().getSheetByName('Locate');

var data = sheet.getDataRange().getValues();
var data_len = data.length;
for(var i=0; i<data_len; i++) {
if(data[17] == "") {
if(data[10] == "PENDING") {
var cell = sheet.getRange(i+1,12).setFormula('H2+G2');
sheet.getRange(i+1,13).setFormula('=C2+M2');
}
}
}
}
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top