Type<\/strong> structure.<\/p>\n\n\n\nType 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\nA 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\nNow, 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\nNow, 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\nCopy 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\nCode explanation<\/strong><\/p>\n\n\n\nThe 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\nThe Long<\/strong> type is used, instead of Int because we deal with big numbers.<\/p>\n\n\n\nThe Cstr<\/strong> function converts the numeral value into a string. Otherwise, the code will result in an error.<\/p>\n\n\n\nThis 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":"\n
Excel VBA Type<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n