{"id":1,"date":"2010-11-26T21:51:49","date_gmt":"2010-11-26T21:51:49","guid":{"rendered":"http:\/\/mariaevert.dk\/VBA\/?p=1"},"modified":"2010-11-29T11:58:35","modified_gmt":"2010-11-29T10:58:35","slug":"hello-world","status":"publish","type":"post","link":"https:\/\/mariaevert.dk\/vba\/?p=1","title":{"rendered":"VBA UserForm &#8211; How to automatically switch to next Textbox"},"content":{"rendered":"<p>The code-snippet presented in this very first post on my blog is helpful when the user has to enter data in TextBoxes in a UserForm, using VBA for Excel. <\/p>\n<p>What we want to achieve is that whenever the user has entered 6 characters in <code>TextBox1<\/code>, then <code>TextBox2 <\/code>is selected by default. That way, the user does not need to used either TAB or the mouse to select the next TextBox: <\/p>\n<p><a href=\"http:\/\/mariaevert.dk\/vba\/wp-content\/uploads\/FindMedarbejder.bmp\"><img width=\"200\" src=\"http:\/\/mariaevert.dk\/vba\/wp-content\/uploads\/FindMedarbejder.bmp\" alt=\"VBA Userform\" title=\"FindMedarbejder\" class=\"alignnone size-full wp-image-26\" \/><\/a><\/p>\n<p>We will need to create a sub that is called whenever a <code>Change<\/code> is made to <code>TextBox1<\/code>. We need to use the <code>TextLength<\/code> and the <code>SetFocus<\/code>, like shown below:  <\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nPrivate Sub TextBox1_Change()\r\n           'whenever there are 6 characters registered in the TextBox\r\n           If TextBox1.TextLength = 6 Then\r\n            'select (SetFocus) the next TextBox\r\n           TextBox2.SetFocus\r\nEnd If\r\n<\/pre>\n<p>Of course, the code-snippet has to be added in the code-behind of the UserForm. Make sure that you have build your Sub correctly by verifying that you have chosen <code>TextBox1 <\/code>and <code>Change<\/code>, and not just <code>General<\/code> at the top of the page: <\/p>\n<p><a href=\"http:\/\/mariaevert.dk\/vba\/wp-content\/uploads\/userform1.png\"><img loading=\"lazy\" src=\"http:\/\/mariaevert.dk\/vba\/wp-content\/uploads\/userform1-300x22.png\" alt=\"\" title=\"userform1\" width=\"350\" height=\"22\" class=\"alignnone size-medium wp-image-32\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The code-snippet presented in this very first post on my blog is helpful when the user has to enter data in TextBoxes in a UserForm, using VBA for Excel. What we want to achieve is that whenever the user has entered 6 characters in TextBox1, then TextBox2 is selected by default. That way, the user [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,5],"tags":[],"_links":{"self":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts\/1"}],"collection":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1"}],"version-history":[{"count":12,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions"}],"predecessor-version":[{"id":4,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts\/1\/revisions\/4"}],"wp:attachment":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}