{"id":245812,"date":"2022-05-18T13:28:03","date_gmt":"2022-05-18T13:28:03","guid":{"rendered":"https:\/\/nursingstudy.org\/vba-in-excel"},"modified":"2025-06-18T04:53:46","modified_gmt":"2025-06-18T04:53:46","slug":"vba-in-excel","status":"publish","type":"post","link":"https:\/\/nursingstudy.org\/examples\/vba-in-excel\/","title":{"rendered":"VBA in Excel"},"content":{"rendered":"<p> I\u2019m trying to study for my Excel course and I need some help to understand this question. <\/p>\n<p><!--googleon: index--> <\/p>\n<p>Problem 1: Super Coder [20 points]<\/p>\n<p>You\u2019ve just joined a new business analytics department and your first assignment involves analyzing an Excel VBA program written by a former employee of the company. This employee, who liked people to call him \u201csuper coder\u201d, wrote a small Excel VBA program that did something with customer account data. The file you\u2019ve inherited is named <strong>FlagOverdueAccts-BigMess.xlsm <\/strong>and it contains a subroutine called procedure1(). The file itself was named by the annoyed employee who inherited the totally uncommented code. <\/p>\n<h2>Part 1.1 \u2013 Cleaning and Commenting<\/h2>\n<p>Thoroughly <strong>comment<\/strong> the code so that anyone with basic VBA experience will know exactly what is going on in the program. <strong>Fix up the indentation<\/strong> and <strong>add appropriate blank lines<\/strong> to make the subroutine more readable. <\/p>\n<p>You\u2019ll notice that Range(\u201cA3\u201d) appears a few times. Modify code to avoid hard coding in the cell reference \u201cA3\u201d).<\/p>\n<p>Several of the variables have \u201cbad variable names\u201d \u2013 <strong>rename<\/strong> them to something more appropriate. In particular, here are the Dim statements for some of the poorly named variables:<\/p>\n<p>Dim cell1 As Range<\/p>\n<p>Dim cell2 As Range<\/p>\n<p>Dim n As Integer<\/p>\n<p>Dim dollars As Currency<\/p>\n<p>Fix the button captions. Rename the procedure1() and procedure2() Subs with more appropriate names<\/p>\n<h2>Part 1.2 \u2013 Program Enhancements<\/h2>\n<p>Modify the first procedure so that:<\/p>\n<ul>\n<li>The results range is sorted in descending order by amount due.<\/li>\n<li>Finish procedure2() so that it clears out the results as intended. Obviously, it needs to work no matter how many or how few rows are in the results table. Rename procedure2 to something more appropriate.<\/li>\n<\/ul>\n<h1>Problem 2: Break Even [20 points]<\/h1>\n<p>Finish up the <strong>BreakEven-W12-Problem.xlsm<\/strong> problem we started in class. See <strong>BreakEven-W12-Demo.xlsm<\/strong> for how it should look and work. Both files are available in the <strong>Downloads-Module05-VBA.zip<\/strong> file. There are also Screencasts on Moodle that walk you through the solution (yes, the answers are in the videos). This problem is designed to introduce you to basic range object referencing and manipulation. To get 15\/20, you must get the \u201cbasic version\u201d working correctly (described in the program comments) and to get 20\/20 you need to get the \u201cabove and beyond version\u201d working correctly. That\u2019s the version that the Demo file illustrates and includes the message when the break even point is detected and the Goal Seek part.<\/p>\n<p><strong><br \/> <\/strong> <\/p>\n<h1>Problem 3: User defined functions for log snooping [20 points]<\/h1>\n<p>I&#8217;ve provided you with a file called access_log.txt. It&#8217;s an Apache server log. For those of you don&#8217;t know, Apache is the primary server software underlying the internet. Learn more at <a href=\"http:\/\/httpd.apache.org\/\" target=\"_blank\" rel=\"noopener\">http:\/\/httpd.apache.org\/<\/a>. You can read about the format of Apache log files at http:\/\/httpd.apache.org\/docs\/1.3\/logs.html#common.<\/p>\n<p>In this problem, you are taking on the role of a security analyst who is checking into some possible irregular traffic hitting your servers.<\/p>\n<p><strong><\/strong><\/p>\n<p>Browse the file to get sense of structure<\/p>\n<ul>\n<li>IP address (or domain name)<\/li>\n<li>date time<\/li>\n<li>HTTP command (GET, POST, HEAD, &#8230;)<\/li>\n<li>Filename\/URL<\/li>\n<li>return code (2xx is good, 3xx is redirect, 4xx is error, 5xx is ?)<\/li>\n<li>data size returned<\/li>\n<\/ul>\n<p><strong>Part 3.1 \u2013 Manually import log file into Excel<\/strong><\/p>\n<p>Import the access_log.txt file into Excel. Import it as a TAB DELIMITED file so that each record gets imported into Column A in your spreadsheet. Here\u2019s a screenshot of the top of my Excel file:<\/p>\n<p><strong><\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p>Save your file as <strong>apache_log_functions.xlsm<\/strong> (yes, as a macro-enabled workbook). You\u2019ll be doing all the work for this problem in this file and you\u2019ll be turning it in as part of the assignment.<\/p>\n<p><strong><\/strong><\/p>\n<p><strong>Part 3.2 \u2013 Creating a function to extract date and time from a log file line<\/strong><\/p>\n<p>Let\u2019s assume you want to be able to quickly filter lines by dates or times of the log line. You\u2019ll see there is a datetime stamp right after a left bracket \u2013 \u201c[\u201c. To make this easy you are going to create a simple VBA function that returns the datetime value for a single line passed into the function. Then you could use this function in a worksheet formula in some column to the right of your data. Each row containing the string of interest will end up with a datetime value and then you could Autofilter the whole range and just see the records of interest. For lines which don\u2019t contain dates, we\u2019ll have our function return a zero.<\/p>\n<p>This function will be called GetApacheDateTime. It will take one input arguments. The first is a string representing an Apache log line. To help you out, here\u2019s a code skeleton. <strong>I STRONGLY SUGGEST YOU USE THIS CODE SKELETON.<\/strong><\/p>\n<p><strong><\/strong><\/p>\n<p>Remember, you can and should debug and test your user defined function using the Immediate Window, or the spreadsheet itself, or a \u201ctester Sub\u201d. See the <strong>Variables and Procedure Basics<\/strong> screencast<strong> <\/strong>which covers creating, using and testing user defined functions. <\/p>\n<p><strong>IMPORTANT<\/strong>: Remember, functions take input arguments and return values. As you can see from the screenshot above, the first input argument is a string. So, your function should NOT reference anything else in the workbook or worksheet other than the argument passed in. That\u2019s the whole point of functions. Also, as you can see from the function declaration, your function will simply return an Excel datetime value. It will NOT explicitly modify the spreadsheet. The spreadsheet will get modified when you use the function in a cell. A user defined function is just like any other Excel function. <\/p>\n<p>Once you have it working, use it in formulas in Column B and then filter so that only those records from March 9, 2004, are shown. Here\u2019s what the answer will look like.<\/p>\n<p><strong><\/strong><\/p>\n<p><strong><br \/> <\/strong> <\/p>\n<p><strong><\/strong><\/p>\n<p><strong>Hacker Extra \u2013 Grabbing the HTTP command<\/strong><\/p>\n<p>While you are pretty excited about your GetApacheDateTime() function, you decide you\u2019d like to also have a function that returns the HTTP command &#8211; e.g. GET or POST or whatever, along with the URL. For example, from a line that looks like this:<\/p>\n<p>195.246.13.119 &#8211; &#8211; [09\/Mar\/2004:01:48:28 -0800] &#8220;GET \/favicon.ico HTTP\/1.1&#8221; 200 1078<\/p>\n<p>you\u2019d like your function to return <\/p>\n<p>GET \/favicon.ico <\/p>\n<p>as a string. Notice we do NOT want the HTTP\/1.1. We just want the HTTP command and the URL. We\u2019ll call this function GetApacheHTTPCommand(). Just like the previous function, it takes a string representing the log line as its only input. As in the previous part, if you get this working, use it in Column C to show me that it works. Here\u2019s my output.<\/p>\n<p>Since this is a Hacker Extra, no screenshot. But, here\u2019s a few hints:<\/p>\n<ul>\n<li>Obviously the HTTP command along with the URL appears between the double quotes.<\/li>\n<li>There\u2019s a space between the HTTP command and the URL.<\/li>\n<li>The URL ends with the last character before a space.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m trying to study for my Excel course and I need some help to understand this question. Problem 1: Super Coder [20 points] You\u2019ve just joined a new business analytics&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wpscppro_dont_share_socialmedia":false,"_wpscppro_custom_social_share_image":0,"_facebook_share_type":"","_twitter_share_type":"","_linkedin_share_type":"","_pinterest_share_type":"","_linkedin_share_type_page":"","_instagram_share_type":"","_medium_share_type":"","_threads_share_type":"","_google_business_share_type":"","_selected_social_profile":[],"_wpsp_enable_custom_social_template":false,"_wpsp_social_scheduling":{"enabled":false,"datetime":null,"platforms":[],"status":"template_only","dateOption":"today","timeOption":"now","customDays":"","customHours":"","customDate":"","customTime":"","schedulingType":"absolute"},"_wpsp_active_default_template":true},"categories":[1061,1],"tags":[],"class_list":["post-245812","post","type-post","status-publish","format-standard","hentry","category-nursing-essays","category-nursing-paper"],"_links":{"self":[{"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/posts\/245812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/comments?post=245812"}],"version-history":[{"count":3,"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/posts\/245812\/revisions"}],"predecessor-version":[{"id":274710,"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/posts\/245812\/revisions\/274710"}],"wp:attachment":[{"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/media?parent=245812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/categories?post=245812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nursingstudy.org\/examples\/wp-json\/wp\/v2\/tags?post=245812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}