{"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 To store currency amounts in a text box you should allow two special characters too like Decimal(.) and Minus Sign(-). <\/p>\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":"\n\n
\u00a0To Restrict Non-Numeric Values in a Text Box<\/h2>\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
\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