{"id":927,"date":"2020-11-28T22:12:18","date_gmt":"2020-11-28T22:12:18","guid":{"rendered":"http:\/\/www.vbastring.com\/blog\/?p=927"},"modified":"2020-11-28T22:13:03","modified_gmt":"2020-11-28T22:13:03","slug":"automatic-invoice-number-generator-excel-vba-project","status":"publish","type":"post","link":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/","title":{"rendered":"Automatic Invoice Number Generator Excel VBA Project"},"content":{"rendered":"<p>This post is actually in response to an Excel VBA question.<\/p>\n<p>Basically it can be summarized by titling it &#8220;automatic invoice number generator excel&#8221;<\/p>\n<p>That is what this is:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/invoice-numbers.png?resize=993%2C552\" alt=\"\" width=\"993\" height=\"552\" class=\"aligncenter size-full wp-image-929\" \/><\/p>\n<p>This can be filled out with user form, but all the data ultimately gets entered into the underlying spreadsheet so we&#8217;ll skip the user form part (let me know if you actually want to see it).<\/p>\n<p>Column A is just a date (&#8220;TODAY&#8217;S DATE&#8221;).<br \/>\nColumn B is a formula based on column A, <em>&#8220;=YEAR(A3)&#8221;<\/em>. (&#8220;YEAR&#8221;)<br \/>\nColumn C is a formula based on column B, <em>&#8220;=RIGHT(B2,2)&#8221;<\/em>. (&#8220;LAST TWO DIGITS OF YEAR&#8221;)<br \/>\nColumn D is just the &#8220;Month&#8221; function based on Column A, but the single digit months are padded <em>&#8220;=IF(LEN(MONTH(A2))<10,\"0\" &#038; MONTH(A2),MONTH(A2))\"<\/em>. (&#8220;MONTH&#8221;)<\/p>\n<p>and finally Column &#8220;E&#8221; is the &#8220;INVOICE NO&#8221; generated with the following code:<\/p>\n<pre class=\"lang:vb decode:true \" >'code by loeblcomservices.com\r\n'713-409-7041\r\n\r\n\r\nSub Rectangle1_Click()\r\n    \r\n    Dim intActiveRow As Integer\r\n    Dim lngLastRow As Long\r\n    Dim strCustomerID As String\r\n    Dim strInvoiceID As String\r\n    \r\n    intActiveRow = ActiveCell.Row\r\n    lngLastRow = FindLastRow(\"A\")\r\n\r\n    strInvoiceID = GetNextInvoiceID(Range(\"C\" &amp; intActiveRow), Range(\"D\" &amp; intActiveRow))\r\n    \r\n    'write the new Invoice ID\r\n    Range(\"E\" &amp; intActiveRow) = strInvoiceID\r\nEnd Sub\r\n\r\n\r\nFunction FindLastRow(WhichColumn As String) As Long\r\n    \r\n    'FINDS THE LAST ROW BASED ON THE COLUMN LETTER &lt;---\r\n    \r\n    Dim lngLastRow As Long\r\n    \r\n    'move to the last row on the worksheet and find the last used cell.\r\n    \r\n    With ActiveSheet\r\n        lngLastRow = ActiveSheet.Cells(1048576, WhichColumn).End(xlUp).Row\r\n    End With\r\n\r\n    FindLastRow = lngLastRow\r\n\r\nEnd Function\r\n\r\n\r\nFunction GetNextInvoiceID(strYear, strMonth) As String\r\n    Dim intCounter As Integer\r\n    Dim varPos As Variant\r\n    Dim lngLastRow As Long\r\n    Dim strCounter As String\r\n    Dim blnFound As Boolean\r\n    \r\n   \r\n    lngLastRow = FindLastRow(\"A\")\r\n    \r\n    'invoice id's stop at 99, add more if you have more than 99 invoices per month\r\n    For intCounter = 1 To 100\r\n        \r\n        'pad the numbers under 10\r\n        If Len(intCounter) &lt; 10 Then\r\n            strCounter = \"0\" &amp; intCounter\r\n        Else\r\n            strCounter = intCounter\r\n        End If\r\n        \r\n        'formulate the trial invoice id to search the list for\r\n        strInvoiceID = strYear &amp; strMonth &amp; strCounter\r\n       \r\n        'if the value is already in the list, the count will be greater than zero.\r\n        intct = Application.WorksheetFunction.CountIf(Range(\"E1:E\" &amp; lngLastRow), strInvoiceID)\r\n        If intct &gt; 0 Then\r\n            'do nothing, because it already is used\r\n        Else\r\n            'it's available, so use it and exit the function\r\n           GetNextInvoiceID = strInvoiceID\r\n           Exit For\r\n        End If\r\n    Next\r\n\r\nEnd Function\r\n<\/pre>\n<p>The real &#8220;work horse&#8221; here is the function <strong>&#8220;GetNextInvoiceID&#8221;<\/strong>.  <\/p>\n<p>It uses a for loop to search Column &#8220;E&#8221; for evidence that the invoice number was used, and it will use the next higher number instead.<\/p>\n<p>What&#8217;s also worth noting is that you can use your normal Excel worksheet functions in your VBA by using, the <em><strong>Application.Worksheetfunction<\/strong><\/em> syntax: ex. <em><strong>Application.WorksheetFunction.CountIf<\/strong><\/em><\/p>\n<p>Let me know if you have questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is actually in response to an Excel VBA question. Basically it can be summarized by titling it &#8220;automatic invoice number generator excel&#8221; That is what this is: This can be filled out with user form, but all the data ultimately gets entered into the underlying spreadsheet so we&#8217;ll skip the user form part [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-927","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Automatic Invoice Number Generator Excel VBA Project - My Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automatic Invoice Number Generator Excel VBA Project - My Blog\" \/>\n<meta property=\"og:description\" content=\"This post is actually in response to an Excel VBA question. Basically it can be summarized by titling it &#8220;automatic invoice number generator excel&#8221; That is what this is: This can be filled out with user form, but all the data ultimately gets entered into the underlying spreadsheet so we&#8217;ll skip the user form part [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/\" \/>\n<meta property=\"og:site_name\" content=\"My Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-28T22:12:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-28T22:13:03+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/invoice-numbers.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"headline\":\"Automatic Invoice Number Generator Excel VBA Project\",\"datePublished\":\"2020-11-28T22:12:18+00:00\",\"dateModified\":\"2020-11-28T22:13:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/\"},\"wordCount\":132,\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/invoice-numbers.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/\",\"name\":\"Automatic Invoice Number Generator Excel VBA Project - My Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/invoice-numbers.png\",\"datePublished\":\"2020-11-28T22:12:18+00:00\",\"dateModified\":\"2020-11-28T22:13:03+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/invoice-numbers.png\",\"contentUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/invoice-numbers.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2020\\\/11\\\/28\\\/automatic-invoice-number-generator-excel-vba-project\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automatic Invoice Number Generator Excel VBA Project\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\",\"name\":\"My Blog\",\"description\":\"My WordPress Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\\\/\\\/vbastring.com\\\/blog\"],\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automatic Invoice Number Generator Excel VBA Project - My Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/","og_locale":"en_US","og_type":"article","og_title":"Automatic Invoice Number Generator Excel VBA Project - My Blog","og_description":"This post is actually in response to an Excel VBA question. Basically it can be summarized by titling it &#8220;automatic invoice number generator excel&#8221; That is what this is: This can be filled out with user form, but all the data ultimately gets entered into the underlying spreadsheet so we&#8217;ll skip the user form part [&hellip;]","og_url":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/","og_site_name":"My Blog","article_published_time":"2020-11-28T22:12:18+00:00","article_modified_time":"2020-11-28T22:13:03+00:00","og_image":[{"url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/invoice-numbers.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/#article","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/"},"author":{"name":"admin","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"headline":"Automatic Invoice Number Generator Excel VBA Project","datePublished":"2020-11-28T22:12:18+00:00","dateModified":"2020-11-28T22:13:03+00:00","mainEntityOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/"},"wordCount":132,"image":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/invoice-numbers.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/","url":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/","name":"Automatic Invoice Number Generator Excel VBA Project - My Blog","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/#primaryimage"},"image":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/invoice-numbers.png","datePublished":"2020-11-28T22:12:18+00:00","dateModified":"2020-11-28T22:13:03+00:00","author":{"@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"breadcrumb":{"@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/#primaryimage","url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/invoice-numbers.png","contentUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2020\/11\/invoice-numbers.png"},{"@type":"BreadcrumbList","@id":"https:\/\/vbastring.com\/blog\/2020\/11\/28\/automatic-invoice-number-generator-excel-vba-project\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vbastring.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Automatic Invoice Number Generator Excel VBA Project"}]},{"@type":"WebSite","@id":"https:\/\/vbastring.com\/blog\/#website","url":"https:\/\/vbastring.com\/blog\/","name":"My Blog","description":"My WordPress Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vbastring.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/vbastring.com\/blog"],"url":"https:\/\/vbastring.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/comments?post=927"}],"version-history":[{"count":5,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":938,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/927\/revisions\/938"}],"wp:attachment":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}