{"id":1998,"date":"2026-04-12T16:02:48","date_gmt":"2026-04-12T08:02:48","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=1998"},"modified":"2026-03-28T04:15:05","modified_gmt":"2026-03-28T04:15:05","slug":"how-to-use-applicationinputbox-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationinputbox-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.InputBox in the xlwings API way"},"content":{"rendered":"\n<p>The Application.InputBox method in Excel VBA is a versatile tool for displaying a dialog box that prompts the user for input. In xlwings, this functionality is exposed through the <code>api<\/code> property, allowing direct access to the underlying Excel object model. This method is particularly useful for creating interactive Excel applications where you need to gather specific information from the user, such as a string, number, cell reference, or even a formula. Unlike a simple input box, it can validate the type of input, making data collection more robust.<\/p>\n\n\n\n<p><strong>Syntax in xlwings:<\/strong><br>The method is accessed via the Application object. The general xlwings API call format is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)<\/code><\/pre>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Prompt<\/code> (Required, String): The message displayed in the dialog box.<\/li>\n\n\n\n<li><code>Title<\/code> (Optional, Variant): The title for the input box window. If omitted, the default title is &#8220;Input&#8221;.<\/li>\n\n\n\n<li><code>Default<\/code> (Optional, Variant): A default value that appears in the text box when the dialog is shown.<\/li>\n\n\n\n<li><code>Left<\/code>, <code>Top<\/code> (Optional, Variant): The screen coordinates (in points) for the upper-left corner of the dialog box.<\/li>\n\n\n\n<li><code>HelpFile<\/code>, <code>HelpContextID<\/code> (Optional, Variant): Identifiers for a custom Help file.<\/li>\n\n\n\n<li><code>Type<\/code> (Optional, Variant): Specifies the return data type. If omitted, it defaults to 0 (a String). This is a critical parameter.<\/li>\n<\/ul>\n\n\n\n<p>The <code>Type<\/code> argument can take the following values:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Value<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>A formula<\/td><\/tr><tr><td>1<\/td><td>A number<\/td><\/tr><tr><td>2<\/td><td>Text (a string)<\/td><\/tr><tr><td>4<\/td><td>A logical value (True or False)<\/td><\/tr><tr><td>8<\/td><td>A cell reference, as a Range object<\/td><\/tr><tr><td>16<\/td><td>An error value, such as #N\/A<\/td><\/tr><tr><td>64<\/td><td>An array of values<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>You can sum these values to allow multiple types (e.g., <code>Type=1+2<\/code> allows both numbers and text). If the user enters data of an incorrect type or clicks &#8220;Cancel&#8221;, the method returns <code>False<\/code>.<\/p>\n\n\n\n<p><strong>Code Examples:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Prompting for a Text String (Default):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\nuser_name = app.api.InputBox(Prompt=\"Enter your name:\", Title=\"User Info\", Type=2)\nif user_name is not False:\n    print(f\"Hello, {user_name}\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Prompting for a Number with a Default Value:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>budget = app.api.InputBox(\"Enter the project budget:\", \"Budget Input\", Default=10000, Type=1)\nif budget is not False:\n    total = budget * 1.1\n    print(f\"Budget with contingency: {total}\")<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Prompting for a Cell Reference (returns an xlwings Range object):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>target_range = app.api.InputBox(\"Select a data range:\", \"Range Selector\", Type=8)\nif target_range is not False:\n    # target_range is an xlwings Range object\n    values = target_range.value\n    print(f\"Selected values: {values}\")<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Allowing Multiple Input Types (Number or Text):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>data = app.api.InputBox(\"Enter ID (number) or Name (text):\", \"Data Entry\", Type=1+2)\nif data is not False:\n    print(f\"Received: {data} (Type: {type(data).__name__})\")<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Application.InputBox method in Excel VBA is a versatile tool for displaying a dialog box that pr&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-1998","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/1998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=1998"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/1998\/revisions"}],"predecessor-version":[{"id":3046,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/1998\/revisions\/3046"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=1998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=1998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}