{"id":12172,"date":"2013-02-18T18:04:37","date_gmt":"2013-02-18T18:04:37","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=2815"},"modified":"2022-08-12T11:18:57","modified_gmt":"2022-08-12T11:18:57","slug":"input-box-and-smart-input-box","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2013\/02\/input-box-and-smart-input-box\/","title":{"rendered":"Input Box and “Smart” Input Box"},"content":{"rendered":"
The most easiest and commonly used function in VBA is Input Box<\/strong>. Without much VBA coding, user can be asked to enter some input at any point of programming. To illustrate the simplicity of Input Box we will take an example. Above Code will add a New sheet in the Excel workbook, with the same name which entered in to the Input Box appeared. InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])<\/span> Title (Optional):<\/span> This is the Title which you want to be displayed in the title bar of the dialog box. If you do not provide title, then application name is displayed in the title bar by default.<\/p>\n Default (Optional):<\/span> This is value which you want to be displayed by default as soon as InputBox is launched. If you do not pass this parameter then it will remain blank by default.<\/p>\n xPos Optional.<\/span> Numeric value which displays the distance of the dialog box from Left side of the Screen. By default it is centrally aligned to the screen.<\/p>\n yPos (Optional):<\/span> Numeric value which displays the distance of the dialog box from upper side of the Screen. By default it is vertically positioned approximately one-third of the way down the screen.<\/p>\n HelpFile (Optional):<\/span> If you want Help button to be displayed on the InputBox then you can pass HelpFile Name in this parameter.<\/p>\n Context (Optional):<\/span> Numeric expression that is the Help context number assigned to the appropriate Help topic by the Help author. If context is provided, helpfile must also be provided.
\nExample: You are trying to add a New WorkSheet in a Workbook but the Sheet Name you want to be provided by the user run time. When I say “Run Time” It means after running the program you are asked for the Name of the sheet and once you enter it a new sheet is created with the same name you entered. You can read more on Adding sheet using VBA in this article<\/a><\/p>\nSub Add_Sheet()\n\n Sheets.Add.Name = InputBox(Title:=\"Title Name\", _\n Prompt:=\"Prompt Name\", _\n Default:=\"Default Value\", _\n xPos:=100, yPos:=100, _\n HelpFile:=\"Demo.help\", _\n Context:=2)\n\nEnd Sub<\/code><\/pre>\n
\nNote: This is a very simple code to add a sheet. It does not have any validation like checking if any sheet with the same name exist or not. To handle all such validation you can read this article.Click Here<\/a><\/strong><\/p>\nSyntax:<\/h3>\n
\nPrompt(Required):<\/span> This is the message which you want to be displayed in the dialog box (Input Dialog Box). For example: “Enter your Sheet Name”<\/p>\n
\nOn Running the above code, InputBox will appear like this:
\n
\n