{"id":9541,"date":"2022-05-04T11:19:14","date_gmt":"2022-05-04T11:19:14","guid":{"rendered":"https:\/\/officetuts.net\/excel\/?p=9541"},"modified":"2024-02-19T15:00:24","modified_gmt":"2024-02-19T15:00:24","slug":"format-date-in-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/","title":{"rendered":"How to Format Date in VBA"},"content":{"rendered":"\n

You are probably familiar right now with the fact that everything that can be done in Excel can also be done through VBA, in most cases even faster.<\/p>\n\n\n\n

Formatting the date is not the exception to this. In the example below, we will show how to do it with the two most used options.<\/p>\n\n\n\n

Format Date in VBA with NumberFormat<\/h2>\n\n\n\n

We use the NumberFormat property<\/strong> to format the numbers that can already be found in our Workbook. We will insert three random numbers<\/a> in column A<\/strong>:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

When opening the workbook, you should be aware that the default format of the numbers is General<\/strong>. We can change the format of these numbers either through the Home tab<\/strong> (number format) or through the VBA:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

To write the code in VBA, we first need to open it. We can do it by clicking ALT + F11<\/strong> on our keyboard to open the VBA<\/strong>, then right-click on the left window and select Insert >> Module<\/strong>:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

Once there, we can set the format of our cells<\/a> with the following code:<\/p>\n\n\n\n

Sub NumberFormats()\n    Range(\"A2\").NumberFormat = \"mm\/dd\/yyyy\"        'short date\n    Range(\"A3\").NumberFormat = \"dddd, mmmm dd, yyyy\"        'long date\n    Range(\"A4\").NumberFormat = \"mmmm,yy\"        'custom date\nEnd Sub<\/code><\/pre>\n\n\n\n

And in the module, it looks like this:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

Once we execute the code by clicking F5<\/strong> on our keyboard, this is what we will end up with our worksheet:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

As seen, the NumberFormat property<\/strong> can be used to format our range in any type of date we want. We can also use the Home tab to find various possible formats of dates, by going to Home >> Number Formatting >> Custom <\/strong>and then scrolling down to find various options for dates:<\/p>\n\n\n\n

\"Graphical<\/figure>\n\n\n\n

We can type any of these formats in the VBA to get the desired results.<\/p>\n\n\n\n

Format Date in VBA with Format Function<\/h2>\n\n\n\n

The NumberFormat <\/strong>can be used for formatting the dates of the cells that are located in the Workbook. In the VBA itself, we can use the Format Function<\/strong> to format dates.<\/p>\n\n\n\n

The format function has two mandatory requirements:<\/p>\n\n\n\n

    \n
  1. Expression<\/strong>– the word or string that we want to format in a certain way.<\/li>\n\n\n\n
  2. Format<\/strong>– the exact format that we want to use.<\/li>\n<\/ol>\n\n\n\n

    When we start to type in the formula, we will see all the possible parameters:<\/p>\n\n\n\n

    \"Graphical<\/figure>\n\n\n\n

    This is the code that we used:<\/p>\n\n\n\n

    Sub FormatFunction()\n    MsgBox Format(Date, \"mm\/dd\/yyyy\")\nEnd Sub<\/code><\/pre>\n\n\n\n

    What this function does is it takes today\u2019s date and formats it in the following way: \u201cmm\/dd\/yyyy\u201d<\/strong>.<\/p>\n\n\n\n

    When we execute the code by pressing F5<\/strong>, this is what will be shown on our screen:<\/p>\n\n\n\n

    \"Graphical<\/figure>\n\n\n\n

    Which is exactly the result we were hoping for.<\/p>\n","protected":false},"excerpt":{"rendered":"

    You are probably familiar right now with the fact that everything that can be done in Excel can also be done through VBA,…<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"yoast_head":"\nHow to Format Date in VBA<\/title>\n<meta name=\"description\" content=\"Learn how to format dates in VBA using NumberFormat property. Increase efficiency of Excel tasks through VBA in a faster way.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Format Date in VBA\" \/>\n<meta property=\"og:description\" content=\"Learn how to format dates in VBA using NumberFormat property. Increase efficiency of Excel tasks through VBA in a faster way.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-04T11:19:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-19T15:00:24+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png\" \/>\n<meta name=\"author\" content=\"Harun Spahic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Harun Spahic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/\"},\"author\":{\"name\":\"Harun Spahic\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\"},\"headline\":\"How to Format Date in VBA\",\"datePublished\":\"2022-05-04T11:19:14+00:00\",\"dateModified\":\"2024-02-19T15:00:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/\"},\"wordCount\":415,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png\",\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/\",\"name\":\"How to Format Date in VBA\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png\",\"datePublished\":\"2022-05-04T11:19:14+00:00\",\"dateModified\":\"2024-02-19T15:00:24+00:00\",\"description\":\"Learn how to format dates in VBA using NumberFormat property. Increase efficiency of Excel tasks through VBA in a faster way.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Format Date in VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/officetuts.net\/excel\/#website\",\"url\":\"https:\/\/officetuts.net\/excel\/\",\"name\":\"\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/officetuts.net\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\",\"name\":\"Tomasz Decker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g\",\"caption\":\"Tomasz Decker\"},\"logo\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\"},\"description\":\"Spreadsheet and Python enthusiast.\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2\",\"name\":\"Harun Spahic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g\",\"caption\":\"Harun Spahic\"},\"sameAs\":[\"http:\/\/human.bsru.ac.th\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Format Date in VBA","description":"Learn how to format dates in VBA using NumberFormat property. Increase efficiency of Excel tasks through VBA in a faster way.","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:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/","og_locale":"en_US","og_type":"article","og_title":"How to Format Date in VBA","og_description":"Learn how to format dates in VBA using NumberFormat property. Increase efficiency of Excel tasks through VBA in a faster way.","og_url":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/","article_published_time":"2022-05-04T11:19:14+00:00","article_modified_time":"2024-02-19T15:00:24+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png"}],"author":"Harun Spahic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Harun Spahic","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/"},"author":{"name":"Harun Spahic","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2"},"headline":"How to Format Date in VBA","datePublished":"2022-05-04T11:19:14+00:00","dateModified":"2024-02-19T15:00:24+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/"},"wordCount":415,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png","articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/","url":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/","name":"How to Format Date in VBA","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png","datePublished":"2022-05-04T11:19:14+00:00","dateModified":"2024-02-19T15:00:24+00:00","description":"Learn how to format dates in VBA using NumberFormat property. Increase efficiency of Excel tasks through VBA in a faster way.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2022\/05\/graphical-user-interface-text-table-description.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/format-date-in-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"How to Format Date in VBA"}]},{"@type":"WebSite","@id":"https:\/\/officetuts.net\/excel\/#website","url":"https:\/\/officetuts.net\/excel\/","name":"","description":"","publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/officetuts.net\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42","name":"Tomasz Decker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/18cbe22837193574870ae40ba56bf712?s=96&d=mm&r=g","caption":"Tomasz Decker"},"logo":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/"},"description":"Spreadsheet and Python enthusiast."},{"@type":"Person","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/694491c70f776081acdac4ae41f222a2","name":"Harun Spahic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b7b887a2249679182be5550a4421a7a9?s=96&d=mm&r=g","caption":"Harun Spahic"},"sameAs":["http:\/\/human.bsru.ac.th\/"]}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9541"}],"collection":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=9541"}],"version-history":[{"count":5,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9541\/revisions"}],"predecessor-version":[{"id":15748,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/9541\/revisions\/15748"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=9541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=9541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=9541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}