Run Google Script through last row only?

qrtback10

New Member
Joined
Jan 19, 2015
Messages
20
Hey guys,

I'm running an email script which works fine but only want it to run through the last row with data in it. I don't need it or want it to run if there is no data obviously. How do I reflect that in the example script below?

For example, say there are only 3 rows of data in my sheet. I only need the script to run for those 3 rows but the code will run for 20 rows. The number of entries to process will vary by the day so I can't just set it at a set number.

Basically I'm looking to replace
Code:
[COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] numRows [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#006666][FONT=inherit]20[/FONT][/COLOR][COLOR=#666600][FONT=inherit];[/FONT][/COLOR][COLOR=#000000][FONT=inherit]   [/FONT][/COLOR][COLOR=#880000][FONT=inherit]// Number of rows to process[/FONT][/COLOR]
with some iteration of .getlastrow...I think?

Code:
[COLOR=#000088][FONT=inherit]function[/FONT][/COLOR][COLOR=#000000][FONT=inherit] sendEmails[/FONT][/COLOR][COLOR=#666600][FONT=inherit]()[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#666600][FONT=inherit]{[/FONT][/COLOR][COLOR=#000000][FONT=inherit]  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] sheet [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#660066][FONT=inherit]SpreadsheetApp[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]getActiveSheet[/FONT][/COLOR][COLOR=#666600][FONT=inherit]();[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] startRow [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#006666][FONT=inherit]2[/FONT][/COLOR][COLOR=#666600][FONT=inherit];[/FONT][/COLOR][COLOR=#000000][FONT=inherit]  [/FONT][/COLOR][COLOR=#880000][FONT=inherit]// First row of data to process[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] numRows [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#006666][FONT=inherit]2[/FONT][/COLOR][COLOR=#666600][FONT=inherit];[/FONT][/COLOR][COLOR=#000000][FONT=inherit]   [/FONT][/COLOR][COLOR=#880000][FONT=inherit]// Number of rows to process[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#880000][FONT=inherit]// Fetch the range of cells A2:B3[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] dataRange [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] sheet[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]getRange[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]startRow[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#006666][FONT=inherit]1[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] numRows[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#006666][FONT=inherit]2[/FONT][/COLOR][COLOR=#666600][FONT=inherit])[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#880000][FONT=inherit]// Fetch values for each row in the Range.[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] data [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] dataRange[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]getValues[/FONT][/COLOR][COLOR=#666600][FONT=inherit]();[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]for[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]i [/FONT][/COLOR][COLOR=#000088][FONT=inherit]in[/FONT][/COLOR][COLOR=#000000][FONT=inherit] data[/FONT][/COLOR][COLOR=#666600][FONT=inherit])[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#666600][FONT=inherit]{[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] row [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] data[/FONT][/COLOR][COLOR=#666600][FONT=inherit][[/FONT][/COLOR][COLOR=#000000][FONT=inherit]i[/FONT][/COLOR][COLOR=#666600][FONT=inherit]];[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] emailAddress [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] row[/FONT][/COLOR][COLOR=#666600][FONT=inherit][[/FONT][/COLOR][COLOR=#006666][FONT=inherit]0[/FONT][/COLOR][COLOR=#666600][FONT=inherit]];[/FONT][/COLOR][COLOR=#000000][FONT=inherit]  [/FONT][/COLOR][COLOR=#880000][FONT=inherit]// First column[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] message [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] row[/FONT][/COLOR][COLOR=#666600][FONT=inherit][[/FONT][/COLOR][COLOR=#006666][FONT=inherit]1[/FONT][/COLOR][COLOR=#666600][FONT=inherit]];[/FONT][/COLOR][COLOR=#000000][FONT=inherit]       [/FONT][/COLOR][COLOR=#880000][FONT=inherit]// Second column[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]var[/FONT][/COLOR][COLOR=#000000][FONT=inherit] subject [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] [/FONT][/COLOR][COLOR=#008800][FONT=inherit]"Sending emails from a Spreadsheet"[/FONT][/COLOR][COLOR=#666600][FONT=inherit];[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#660066][FONT=inherit]MailApp[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]sendEmail[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]emailAddress[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] subject[/FONT][/COLOR][COLOR=#666600][FONT=inherit],[/FONT][/COLOR][COLOR=#000000][FONT=inherit] message[/FONT][/COLOR][COLOR=#666600][FONT=inherit]);[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#666600][FONT=inherit]}[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
[/FONT][/COLOR][COLOR=#666600][FONT=inherit]}[/FONT][/COLOR]

Thanks for your help!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Figured it out! For anyone wondering, this is what it looks like:

Code:
[COLOR=#00008B][FONT=Consolas]function[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] sendEmails[/FONT][/COLOR][COLOR=#000000][FONT=Consolas](){[/FONT][/COLOR][COLOR=#000000]
  [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] sheet [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] [/COLOR][COLOR=#2B91AF]SpreadsheetApp[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]getActiveSheet[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
  [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] startRow [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]2[/COLOR][COLOR=#000000];[/COLOR][COLOR=#808080]// First row of data to process[/COLOR][COLOR=#000000]
  [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] data [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] sheet[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]getDataRange[/COLOR][COLOR=#000000]().[/COLOR][COLOR=#000000]getValues[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]

  [/COLOR][COLOR=#00008B]for[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] i[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000]startRow[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000] i[/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000]data[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]length[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000] i[/COLOR][COLOR=#000000]++){[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] row [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] data[/COLOR][COLOR=#000000][[/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]];[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] emailAddress [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] row[/COLOR][COLOR=#000000][[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]];[/COLOR][COLOR=#808080]// First column[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] message [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] row[/COLOR][COLOR=#000000][[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]];[/COLOR][COLOR=#808080]// Second column[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]var[/COLOR][COLOR=#000000] subject [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]"Sending emails from a Spreadsheet"[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#2B91AF]MailApp[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]sendEmail[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]emailAddress[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] subject[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] message[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
  [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
 [/COLOR][COLOR=#000000][FONT=Consolas]}[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,963
Members
449,200
Latest member
indiansth

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