{"id":2422,"date":"2026-11-10T15:28:59","date_gmt":"2026-11-10T07:28:59","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2422"},"modified":"2026-03-29T10:36:13","modified_gmt":"2026-03-29T10:36:13","slug":"how-to-use-rangerun-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangerun-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.Run in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Run<\/code> member of the <code>Range<\/code> object in the Excel object model is accessible via xlwings and is used to execute a macro or a user-defined function (UDF) that is associated with a specific range. This is particularly useful for running custom VBA code that is tied to a worksheet event or a specific cell action. In xlwings, this functionality is exposed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong>:<br>The primary function of <code>Range.Run<\/code> is to execute a macro or function. It can be used to trigger VBA procedures that are defined in the workbook. This allows for automation and integration of custom business logic directly from Python scripts using xlwings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong>:<br>In xlwings, the syntax for calling the <code>Run<\/code> method on a <code>Range<\/code> object is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_object.api.Run(Arg1, Arg2, ..., Arg30)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>range_object<\/code>: This is the xlwings Range object representing the cell or range in Excel.<\/li>\n\n\n\n<li><code>Arg1, Arg2, ..., Arg30<\/code>: These are optional arguments that can be passed to the macro or function being executed. They correspond to the parameters expected by the VBA procedure. You can pass up to 30 arguments, mirroring Excel&#8217;s VBA capabilities.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The arguments are passed by value, and their types should match what the VBA code expects (e.g., strings, numbers). If no arguments are needed, you can call the method without any.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong>:<br>Suppose you have an Excel workbook with a VBA macro named <code>ProcessData<\/code> that takes two arguments: a string for a report name and a number for a threshold. The macro is designed to process data in a specific range. Using xlwings, you can execute this macro from Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, ensure the workbook is open and connected via xlwings. Then, you can use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active workbook or open a specific one\nwb = xw.books.active # Assumes Excel is running and workbook is open\nws = wb.sheets&#91;'Sheet1']\n\n# Define the range where the macro is associated (if applicable) or any range\ntarget_range = ws.range('A1')\n\n# Execute the macro with arguments\ntarget_range.api.Run(\"ProcessData\", \"MonthlyReport\", 100)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Run` member of the `Range` object in the Excel object model is accessible via xlwings and is us&#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-2422","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2422","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=2422"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2422\/revisions"}],"predecessor-version":[{"id":3679,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2422\/revisions\/3679"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}