{"id":2363,"date":"2026-10-12T07:55:16","date_gmt":"2026-10-11T23:55:16","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2363"},"modified":"2026-03-29T09:55:06","modified_gmt":"2026-03-29T09:55:06","slug":"how-to-use-rangeapplynames-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangeapplynames-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.ApplyNames in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>ApplyNames<\/code> method in Excel&#8217;s object model is a powerful feature for applying a list of defined names to formulas within a specified range, replacing cell references with their corresponding named ranges where applicable. This enhances formula readability and maintainability. In xlwings, this functionality is accessed through the <code>api<\/code> property of a Range object, providing direct access to the underlying Excel VBA object model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary purpose of <code>ApplyNames<\/code> is to scan formulas in a given range and substitute direct cell references (like <code>A1<\/code> or <code>$B$2<\/code>) with defined names that refer to those cells. For example, if you have a named range &#8220;SalesData&#8221; referring to <code>B2:B10<\/code>, a formula like <code>=SUM(B2:B10)<\/code> within the specified range can be automatically converted to <code>=SUM(SalesData)<\/code>. This method only applies names that are defined in the workbook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>The xlwings API call mirrors the VBA syntax closely. The method is called on a Range object via its <code>api<\/code> property.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_object.api.ApplyNames(Names, IgnoreRelativeAbsolute, UseRowColumnNames, OmitColumn, OmitRow, Order, AppendLast)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Names<\/code>: An optional variant. An array of defined names to apply. If omitted, all names in the workbook are applied.<\/li>\n\n\n\n<li><code>IgnoreRelativeAbsolute<\/code>: An optional variant. If <code>True<\/code>, the method ignores the relative or absolute status of names and references. Default is <code>True<\/code>.<\/li>\n\n\n\n<li><code>UseRowColumnNames<\/code>: An optional variant. If <code>True<\/code>, the method uses the row and column names of the range if no defined name exists. Default is <code>True<\/code>.<\/li>\n\n\n\n<li><code>OmitColumn<\/code>: An optional variant. If <code>True<\/code>, replaces the entire reference with the column name if the row portion of the reference matches the named range. Default is <code>False<\/code>.<\/li>\n\n\n\n<li><code>OmitRow<\/code>: An optional variant. If <code>True<\/code>, replaces the entire reference with the row name if the column portion of the reference matches the named range. Default is <code>False<\/code>.<\/li>\n\n\n\n<li><code>Order<\/code>: An optional variant. Determines the order for evaluating row and column names. Use <code>xlRowThenColumn<\/code> (1) or <code>xlColumnThenRow<\/code> (2).<\/li>\n\n\n\n<li><code>AppendLast<\/code>: An optional variant. If <code>True<\/code>, the row and column names are appended to the existing defined name. Default is <code>False<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Example:<\/strong><br>The following xlwings script demonstrates using <code>ApplyNames<\/code>. It assumes an active workbook with defined names and formulas in a specific range.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance and workbook\napp = xw.apps.active\nwb = app.books.active\n\n# Define a target range, e.g., from A1 to C10 on the first sheet\ntarget_range = wb.sheets&#91;0].range(\"A1:C10\")\n\n# Apply all defined names in the workbook to formulas in the target range.\n# We use default parameters for IgnoreRelativeAbsolute and UseRowColumnNames.\ntarget_range.api.ApplyNames()\n\n# To apply only specific names, provide an array.\n# For instance, applying only the names \"Revenue\" and \"Costs\":\ntarget_range.api.ApplyNames(Names=&#91;\"Revenue\", \"Costs\"])\n\n# Example with more parameters: Apply names, ignoring relative\/absolute status,\n# not using row\/column names, and evaluating column names before row names.\ntarget_range.api.ApplyNames(IgnoreRelativeAbsolute=True,\nUseRowColumnNames=False,\nOrder=2) # 2 corresponds to xlColumnThenRow<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `ApplyNames` method in Excel&apos;s object model is a powerful feature for applying a list of defined&#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-2363","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2363","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=2363"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2363\/revisions"}],"predecessor-version":[{"id":3602,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2363\/revisions\/3602"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}