{"id":12069,"date":"2011-10-11T11:11:14","date_gmt":"2011-10-11T11:11:14","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=43"},"modified":"2022-09-08T08:35:53","modified_gmt":"2022-09-08T08:35:53","slug":"restrict-some-key-or-characters-in-text-box-in-a-excel-macro","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/","title":{"rendered":"Restrict some key or Characters in text box in a Excel Macro"},"content":{"rendered":"

[et_pb_section fb_built=”1″ admin_label=”section” _builder_version=”4.16″ da_disable_devices=”off|off|off” global_colors_info=”{}” da_is_popup=”off” da_exit_intent=”off” da_has_close=”on” da_alt_close=”off” da_dark_close=”off” da_not_modal=”on” da_is_singular=”off” da_with_loader=”off” da_has_shadow=”on”][et_pb_row admin_label=”row” _builder_version=”4.16″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” global_colors_info=”{}”][et_pb_column type=”4_4″ _builder_version=”4.16″ custom_padding=”|||” global_colors_info=”{}” custom_padding__hover=”|||”][et_pb_text _builder_version=”4.17.6″ _module_preset=”default” global_colors_info=”{}”]<\/p>\n

When you are using Text Boxes in Excel Macros then there are some instances where you want to restrict some entries in the Text Box. <\/p>\n

For example<\/strong>, in A Textbox where you are entering Amount. Here you may need that you should be able to Enter All numeric characters from 0-9 and also Decimal (.) and Minus Sign(-).<\/p>\n

You don’t have any inbuilt functionality in Excel Textbox to restrict it to only Numeric Values. However, by writing a piece of Code under KeyPress Event of Textbox<\/strong>, you can achieve this.<\/p>\n

Just copy-paste the below-mentioned code under KeyPress Event of the Text Box.<\/div>\n
    \n
  1. \n

    \u00a0To Restrict Non-Numeric Values in a Text Box<\/h2>\n

    To store currency amounts in a text box you should allow two special characters too like Decimal(.) and Minus Sign(-). <\/p>\n

    \nPrivate Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)\n    Select Case KeyAscii\n        Case Asc(\"0\") To Asc(\"9\")\n        Case Asc(\"-\")\n            If InStr(1, TextBox1.Text, \"-\") > 0 Or TextBox1.SelStart > 0 Then\n                KeyAscii = 0\n            End If\n        Case Asc(\".\")\n            If InStr(1, Me.TextBox1.Text, \".\") > 0 Then\n                KeyAscii = 0\n            End If\n        Case Else\n            KeyAscii = 0\n    End Select\nEnd Sub\n<\/pre>\n<\/li>\n
  2. \n

    \u00a0To Restrict All Keys Except Alphabets in Text Box in Excel Macro<\/h2>\n
    \nPrivate Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)\n    Select Case KeyAscii\n        Case Asc(\"a\") To Asc(\"z\")\n        Case Asc(\"A\") To Asc(\"Z\")\n        Case Else\n            KeyAscii = 0\n    End Select\nEnd Sub\n\n<\/pre>\n<\/li>\n<\/ol>\n

    [\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"

    When you are using Text Boxes in Excel Macros then there are some instances where you want to restrict some entries in the Text Box. For example, in A Textbox where you are entering Amount. Here you may need that you should be able to Enter All numeric characters from 0-9 and also Decimal (.) […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1246,1682],"tags":[],"class_list":["post-12069","post","type-post","status-publish","format-standard","hentry","category-macro","category-popular-articles"],"yoast_head":"\nRestrict some key or Characters in text box in a Excel Macro - Let's excel in Excel<\/title>\n<meta name=\"description\" content=\"KeyPress Event of a Text Box in Excel macro. Restrict some keys or characters in an excel text box. Numeric Values in Text Box only.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Restrict some key or Characters in text box in a Excel Macro\" \/>\n<meta property=\"og:description\" content=\"KeyPress Event of a Text Box in Excel macro. Restrict some keys or characters in an excel text box. Numeric Values in Text Box only.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/\" \/>\n<meta property=\"og:site_name\" content=\"Let's excel in Excel\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2011-10-11T11:11:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-08T08:35:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/vmlogger.com_-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\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:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"Restrict some key or Characters in text box in a Excel Macro\",\"datePublished\":\"2011-10-11T11:11:14+00:00\",\"dateModified\":\"2022-09-08T08:35:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/\"},\"wordCount\":269,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"articleSection\":[\"Excel Macro\",\"Popular Articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/\",\"name\":\"Restrict some key or Characters in text box in a Excel Macro - Let's excel in Excel\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"datePublished\":\"2011-10-11T11:11:14+00:00\",\"dateModified\":\"2022-09-08T08:35:53+00:00\",\"description\":\"KeyPress Event of a Text Box in Excel macro. Restrict some keys or characters in an excel text box. Numeric Values in Text Box only.\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Macro\",\"item\":\"https:\/\/vmlogger.com\/excel\/macro\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Restrict some key or Characters in text box in a Excel Macro\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let's excel in Excel\",\"description\":\"Let's share knowledge\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/excel\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"width\":528,\"height\":560,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Restrict some key or Characters in text box in a Excel Macro - Let's excel in Excel","description":"KeyPress Event of a Text Box in Excel macro. Restrict some keys or characters in an excel text box. Numeric Values in Text Box only.","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:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/","og_locale":"en_US","og_type":"article","og_title":"Restrict some key or Characters in text box in a Excel Macro","og_description":"KeyPress Event of a Text Box in Excel macro. Restrict some keys or characters in an excel text box. Numeric Values in Text Box only.","og_url":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/","og_site_name":"Let's excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2011-10-11T11:11:14+00:00","article_modified_time":"2022-09-08T08:35:53+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/vmlogger.com_-1.png","type":"image\/png"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"Restrict some key or Characters in text box in a Excel Macro","datePublished":"2011-10-11T11:11:14+00:00","dateModified":"2022-09-08T08:35:53+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/"},"wordCount":269,"commentCount":0,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"articleSection":["Excel Macro","Popular Articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/","url":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/","name":"Restrict some key or Characters in text box in a Excel Macro - Let's excel in Excel","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"datePublished":"2011-10-11T11:11:14+00:00","dateModified":"2022-09-08T08:35:53+00:00","description":"KeyPress Event of a Text Box in Excel macro. Restrict some keys or characters in an excel text box. Numeric Values in Text Box only.","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2011\/10\/restrict-some-key-or-characters-in-text-box-in-a-excel-macro\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Macro","item":"https:\/\/vmlogger.com\/excel\/macro\/"},{"@type":"ListItem","position":3,"name":"Restrict some key or Characters in text box in a Excel Macro"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let's excel in Excel","description":"Let's share knowledge","publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/excel\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","width":528,"height":560,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12069"}],"collection":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/comments?post=12069"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12069\/revisions"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=12069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=12069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=12069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}