{"id":3509,"date":"2018-11-24T15:41:44","date_gmt":"2018-11-24T15:41:44","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=3509"},"modified":"2024-02-19T15:00:35","modified_gmt":"2024-02-19T15:00:35","slug":"custom-data-types","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/","title":{"rendered":"Excel VBA Type"},"content":{"rendered":"\n

In VBA, you can create your own data types using the Type<\/strong> keyword. It\u2019s very similar to struct data type from other programming languages.<\/p>\n\n\n\n

It consists of one or multiple existing types into one new type. A good example of using this data types<\/a> is a database record. If you want to keep a record of your employees, it\u2019s a good idea to use the Type<\/strong> structure.<\/p>\n\n\n\n

Type Employee\n    first_name As String\n    last_name As String\n    id_number As Integer\n    salary As Long\nEnd Type<\/pre>\n\n\n\n

A variable declared as Employee will have all these data types built-in. You declare user-defined types the same way you declare standard built-in VBA types.<\/p>\n\n\n\n

Dim emp As Employee<\/pre>\n\n\n\n

Now, you can access individual fields of this structure by using the dot (.) operator. You can assign a value to any of the Employee types.<\/p>\n\n\n\n

emp.first_name = \"John\"\nemp.last_name = \"Brown\"\nemp.id_number = 1\nemp.salary = 50000<\/pre>\n\n\n\n

Now, you can display these elements. In this example, we are going to do it inside MsgBox.<\/p>\n\n\n\n

In order to run this function, you have to create a new module inside the VBA editor (Left Alt + F11<\/strong>).<\/p>\n\n\n\n

Copy the following code into the module you created.<\/p>\n\n\n\n

Type Employee\n    first_name As String\n    last_name As String\n    id_number As Integer\n    salary As Long\nEnd Type\n\nSub CheckEmployee()\n    Dim emp As Employee\n    emp.first_name = \"John\"\n    emp.last_name = \"Brown\"\n    emp.id_number = 1\n    emp.salary = 50000\n    \n    MsgBox \"Employee \" + emp.first_name + \" \" + emp.last_name + \", identified by id \" + CStr(emp.id_number) + \" earns \" + CStr(emp.salary) + \" dollars a year.\"\nEnd Sub<\/pre>\n\n\n\n

Code explanation<\/strong><\/p>\n\n\n\n

The code is pretty straightforward. What probably needs explanation is the Long<\/strong> type in salary and the  Cstr<\/strong> function used in MsgBox.<\/p>\n\n\n\n

The Long<\/strong> type is used, instead of Int because we deal with big numbers.<\/p>\n\n\n\n

The Cstr<\/strong> function converts the numeral value into a string. Otherwise, the code will result in an error.<\/p>\n\n\n\n

This code will return the following result.<\/p>\n\n\n\n

\"\"<\/figure>\n","protected":false},"excerpt":{"rendered":"

In VBA, you can create your own data types using the Type keyword. It\u2019s very similar to struct data type from other programming…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"yoast_head":"\nExcel VBA Type<\/title>\n<meta name=\"description\" content=\"Learn how to create custom data types in VBA using the Type keyword. Combine multiple types into one for improved organization and efficiency.\" \/>\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\/custom-data-types\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Type\" \/>\n<meta property=\"og:description\" content=\"Learn how to create custom data types in VBA using the Type keyword. Combine multiple types into one for improved organization and efficiency.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-24T15:41:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-19T15:00:35+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png\" \/>\n<meta name=\"author\" content=\"Tomasz Decker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tomasz Decker\" \/>\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\/custom-data-types\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/\"},\"author\":{\"name\":\"Tomasz Decker\",\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"headline\":\"Excel VBA Type\",\"datePublished\":\"2018-11-24T15:41:44+00:00\",\"dateModified\":\"2024-02-19T15:00:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/\"},\"wordCount\":232,\"publisher\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png\",\"articleSection\":[\"vba\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/\",\"url\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/\",\"name\":\"Excel VBA Type\",\"isPartOf\":{\"@id\":\"https:\/\/officetuts.net\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png\",\"datePublished\":\"2018-11-24T15:41:44+00:00\",\"dateModified\":\"2024-02-19T15:00:35+00:00\",\"description\":\"Learn how to create custom data types in VBA using the Type keyword. Combine multiple types into one for improved organization and efficiency.\",\"breadcrumb\":{\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage\",\"url\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png\",\"contentUrl\":\"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/officetuts.net\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Type\"}]},{\"@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.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Type","description":"Learn how to create custom data types in VBA using the Type keyword. Combine multiple types into one for improved organization and efficiency.","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\/custom-data-types\/","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Type","og_description":"Learn how to create custom data types in VBA using the Type keyword. Combine multiple types into one for improved organization and efficiency.","og_url":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/","article_published_time":"2018-11-24T15:41:44+00:00","article_modified_time":"2024-02-19T15:00:35+00:00","og_image":[{"url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png"}],"author":"Tomasz Decker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Tomasz Decker","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#article","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/"},"author":{"name":"Tomasz Decker","@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"headline":"Excel VBA Type","datePublished":"2018-11-24T15:41:44+00:00","dateModified":"2024-02-19T15:00:35+00:00","mainEntityOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/"},"wordCount":232,"publisher":{"@id":"https:\/\/officetuts.net\/excel\/#\/schema\/person\/41b0b6996aaa4c4127f86f3d24452d42"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png","articleSection":["vba"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/","url":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/","name":"Excel VBA Type","isPartOf":{"@id":"https:\/\/officetuts.net\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage"},"image":{"@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage"},"thumbnailUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png","datePublished":"2018-11-24T15:41:44+00:00","dateModified":"2024-02-19T15:00:35+00:00","description":"Learn how to create custom data types in VBA using the Type keyword. Combine multiple types into one for improved organization and efficiency.","breadcrumb":{"@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#primaryimage","url":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png","contentUrl":"http:\/\/officetuts.net\/excel\/wp-content\/uploads\/sites\/2\/2018\/11\/msx-box-data-type.png"},{"@type":"BreadcrumbList","@id":"https:\/\/officetuts.net\/excel\/vba\/custom-data-types\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/officetuts.net\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Type"}]},{"@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."}]}},"_links":{"self":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/3509"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/comments?post=3509"}],"version-history":[{"count":4,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/3509\/revisions"}],"predecessor-version":[{"id":12992,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/posts\/3509\/revisions\/12992"}],"wp:attachment":[{"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/media?parent=3509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/categories?post=3509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/officetuts.net\/excel\/wp-json\/wp\/v2\/tags?post=3509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}