{"id":5394,"date":"2013-05-05T10:24:24","date_gmt":"2013-05-05T16:24:24","guid":{"rendered":"http:\/\/sanuja.com\/blog\/?p=5394"},"modified":"2020-05-31T13:46:00","modified_gmt":"2020-05-31T19:46:00","slug":"extract-data-from-rows-for-email","status":"publish","type":"post","link":"https:\/\/sanuja.com\/blog\/extract-data-from-rows-for-email","title":{"rendered":"Extract data from rows for email"},"content":{"rendered":"<p>If you are a teacher or a professor, you probably have several students in your class. Let&#8217;s say you would like to provide them with an update on their averages after every single quiz and exam they take. But you hate to email each and everyone of them because it consumes a lot of time to copy data from your Excel file of student records to individual emails. This is where the extract data from row by row using loop iteration comes to save you.<\/p>\n<p> <!--more--><\/p>\n<p>At the beginning of the year you may have created a spreadsheet with student names, email address, averages for each and every exam and maybe even student numbers. The following example is created to show such Excel file (exception to student ID, which you can enter in a new column).<br \/>\n<figure id=\"attachment_5410\" aria-describedby=\"caption-attachment-5410\" style=\"width: 487px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" data-attachment-id=\"5410\" data-permalink=\"https:\/\/sanuja.com\/blog\/extract-data-from-rows-for-email\/excel_line_loop\" data-orig-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop.gif\" data-orig-size=\"487,243\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"excel_line_loop\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Sheet for the loop iteration in VisualBasic.&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop.gif\" data-large-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop.gif\" src=\"http:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop.gif\" alt=\"Sheet for the loop iteration in VisualBasic.\" width=\"487\" height=\"243\" class=\"size-full wp-image-5410\" \/><figcaption id=\"caption-attachment-5410\" class=\"wp-caption-text\">Sheet for the loop iteration in VisualBasic.<\/figcaption><\/figure><\/p>\n<h2>VB Script<\/h2>\n<p>If you have not work with VB Scripts before, please read the introduction article, <a href=\"http:\/\/sanuja.com\/blog\/excel-visual-basic-scripts\/\">here<\/a>. I assume you know the fundamentals of Excel Macros. You may copy and paste the following VB Script to your Excel file OR download the file from here. (I will upload the file tonight!).<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">Private Declare Function ShellExecute Lib \"shell32.dll\" _\r\nAlias \"ShellExecuteA\" (ByVal hwnd As Long, ByVal lpOperation As String, _\r\nByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _\r\nByVal nShowCmd As Long) As Long\r\nSub SendEMail()\r\n    Dim Email As String, Subj As String\r\n    Dim Msg As String, URL As String\r\n    Dim r As Integer, x As Double\r\n    For r = 2 To 150 'Looping the rows 2-150, extracting data one row at a time'\r\n'       Extract the email address found in every row  (limit defined in the for loop above) at cell number 2\r\n        Email = Cells(r, 2)\r\n        \r\n'       Message subject\r\n        Subj = \"Your final exam grade are up!\"\r\n\r\n'       Compose the message\r\n        Msg = \"\"\r\n        Msg = Msg & \"Dear \" & Cells(r, 1) & \",\" & vbCrLf & vbCrLf\r\n        Msg = Msg & \"I am pleased to inform you that we have completed submitting your exam averages as follows...\" & vbCrLf\r\n        Msg = Msg & \"Quiz No. 1: \"\r\n        Msg = Msg & Cells(r, 3).Text & \",\" & vbCrLf\r\n        Msg = Msg & \"Quiz No. 2: \"\r\n        Msg = Msg & Cells(r, 4).Text & \",\" & vbCrLf\r\n        Msg = Msg & \"Quiz No. 3: \"\r\n        Msg = Msg & Cells(r, 5).Text & \",\" & vbCrLf\r\n        Msg = Msg & \"Overall Average: \"\r\n        Msg = Msg & Cells(r, 6).Text & \".\" & vbCrLf & vbCrLf\r\n        Msg = Msg & \"Sanuja Senanayake\" & vbCrLf\r\n        Msg = Msg & \"Instructor\"\r\n        \r\n'       Replace spaces with %20 (hex)\r\n        Subj = Application.WorksheetFunction.Substitute(Subj, \" \", \"%20\")\r\n        Msg = Application.WorksheetFunction.Substitute(Msg, \" \", \"%20\")\r\n                \r\n'       Replace carriage returns with %0D%0A (hex)\r\n        Msg = Application.WorksheetFunction.Substitute(Msg, vbCrLf, \"%0D%0A\")\r\n'       Create the URL\r\n        URL = \"mailto:\" & Email & \"?subject=\" & Subj & \"&body=\" & Msg\r\n\r\n'       Execute the URL (start the email client)\r\n        ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus\r\n\r\n'       Wait two seconds before sending keystrokes\r\n        Application.Wait (Now + TimeValue(\"0:00:02\"))\r\n        Application.SendKeys \"%s\"\r\n    Next r\r\nEnd Sub<\/pre>\n<p>There are several impotent codes in this program. The first line is a command to open <em>shell32.dll <\/em>which is the Windows default email. Depend on which program is set as your default, it may open any email program such as Outlook, LiveMail, Thunderbird, etc. <em>Please be advised that once this program is &#8220;RUN&#8221;, it will automatically start creating email temples and  send them. You cannot stop and check unless you force terminate it or it comes across an error.<\/em><\/p>\n<p>The <em>Sub SendEMail()<\/em> is calling in the function to automate email processing. A function is like a box which contains data specific to an operation. In this perticular example, the box starts at <em>Sub SendEMail()<\/em> at line 5 and it ends at <em>End Sub<\/em> at line 47. Between the lines 5 and 47, we excite several commands in one of which is a <u>for loop<\/u>. The for loop is a very impotent iteration command. But I don&#8217;t want to go into detail since it is widely used in several different programming languages. What it does is to repeat few operations over and over for the given set window. The window in this case is defined as <em>r = 2 To 150<\/em>, which is rows 2 to 150 (in other words, if each row is for a new student, 148 students can be covered). We started iteration from row 2, because the first row is used for titles; Name, Email, Quiz# and Overall.<\/p>\n<p>But what if you don&#8217;t have 148 students? No problem. Any VB Script like this can be modified to serve specific needs. All you have to do is to change the iteration window to any value you like in the <em>for loop<\/em>. Let&#8217;s say you only have 20 students, rewrite the <em>for loop<\/em> as <em>For r = 2 To 22<\/em>. Now you have few students who decided to join the last later than others. So, you decided to enter their names at the very end of the file. If you wan to send an email with their grades, but you do not want to send the same email to all the students, you can change the upper limit of the for loop to avoid unnecessary emails. For example,  <em>For r = 151 To 162<\/em> will only extract data from rows 151 to 162 and compose emails accordingly.<\/p>\n<p>Next, you have five different quizzes, a midterm and a final exam. To use this script, simply add more cell reader in the the for loop extracting the cell you want. It is as simple as adding <em>Msg = Msg &#038; Cells(r, XXX).Text &#038; &#8220;,&#8221; &#038; vbCrLf<\/em> in the current <em>for loop<\/em> , where  XXX is the cell number.<\/p>\n<p>There are many other modifications you can do with this type of script. However, I am not going to spoon feed you on flexibility of VB. Instead, I encourage you to come up with your own modifications. The script is free to copy, modify, reuse and republish with no credit to sanuja.com. To give you an idea on what you can do with it, try these;<\/p>\n<ul>\n<li>change the plain text to <strong>bold<\/strong>, <em>italicized<\/em>, etc texts in the email body<\/li>\n<li>replace the <em>for loop<\/em> with a <em>while loop<\/em> to prevent run on program<\/li>\n<li>create an <em>if<\/em> statement in the for\/while loop to prevent invalid email address errors (exceptions)<\/li>\n<li>check for multiple email addresses on the machine and generate a pop-up before run the loop to give user the choice<\/li>\n<li>personalized the email by only taking the first word in the name column (avoiding the full name)<\/li>\n<li>&#8230;and many more creative ideas!<\/li>\n<\/ul>\n<h2>Sample Email<\/h2>\n<p>Here is an example of this script generated email.<br \/>\n<figure id=\"attachment_5415\" aria-describedby=\"caption-attachment-5415\" style=\"width: 638px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" data-attachment-id=\"5415\" data-permalink=\"https:\/\/sanuja.com\/blog\/extract-data-from-rows-for-email\/excel_line_loop_email\" data-orig-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop_email.gif\" data-orig-size=\"638,295\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"excel_line_loop_email\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Email Format&lt;\/p&gt;\n\" data-medium-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop_email.gif\" data-large-file=\"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop_email.gif\" src=\"http:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop_email.gif\" alt=\"Email Format\" width=\"638\" height=\"295\" class=\"size-full wp-image-5415\" \/><figcaption id=\"caption-attachment-5415\" class=\"wp-caption-text\">Email Format<\/figcaption><\/figure><br \/>\nIf you have questions on customization and you don&#8217;t understand my comments within the script, fire me an email and I will do my best to help you out. You can also read more Visual Basic articles <a href=\"http:\/\/sanuja.com\/blog\/tag\/visualbasic\/\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a teacher or a professor, you probably have several students in your class. Let&#8217;s say you would like to provide them with an update on their averages after every single quiz and exam they take. But you hate to email each and everyone of them because it consumes a lot of time &hellip; <a href=\"https:\/\/sanuja.com\/blog\/extract-data-from-rows-for-email\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Extract data from rows for email<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":5415,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[31],"class_list":["post-5394","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","tag-visualbasic"],"jetpack_featured_media_url":"https:\/\/sanuja.com\/blog\/wp-content\/uploads\/2013\/05\/excel_line_loop_email.gif","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/5394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/comments?post=5394"}],"version-history":[{"count":0,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/posts\/5394\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media\/5415"}],"wp:attachment":[{"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/media?parent=5394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/categories?post=5394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanuja.com\/blog\/wp-json\/wp\/v2\/tags?post=5394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}