{"id":2016,"date":"2026-04-21T16:27:07","date_gmt":"2026-04-21T08:27:07","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2016"},"modified":"2026-03-28T04:55:28","modified_gmt":"2026-03-28T04:55:28","slug":"how-to-use-applicationunion-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationunion-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.Union in the xlwings API way"},"content":{"rendered":"\n<p>The <code>Application.Union<\/code> method in Excel VBA is used to create a single, combined range from two or more individual ranges. This combined range object can then be used for subsequent operations, such as formatting or data manipulation, applied uniformly across all the included cells. In xlwings, this functionality is accessed through the <code>api<\/code> property of an xlwings object, which provides direct access to the underlying Excel object model. This allows Python scripts to leverage Excel&#8217;s powerful range combination logic seamlessly.<\/p>\n\n\n\n<p><strong>Functionality<\/strong><br>The primary function of <code>Union<\/code> is to create a composite <code>Range<\/code> object. This is particularly useful when you need to perform the same action on multiple, non-contiguous cell blocks without having to loop through each range separately. It streamlines code and improves efficiency.<\/p>\n\n\n\n<p><strong>Syntax in xlwings<\/strong><br>The syntax follows the pattern of accessing the VBA method through the xlwings <code>api<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>combined_range = xw.apps&#91;0].api.Union(Range1, Range2, ...)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>xw.apps[0].api<\/code>: This accesses the <code>Application<\/code> object of the first open Excel instance via xlwings.<\/li>\n\n\n\n<li><code>.Union()<\/code>: The method call.<\/li>\n\n\n\n<li><strong>Parameters:<\/strong> <code>Range1<\/code>, <code>Range2<\/code>, \u2026: These are two or more <code>Range<\/code> objects that you want to combine. You must provide at least two <code>Range<\/code> arguments. These ranges can refer to different worksheets or even different workbooks.<\/li>\n\n\n\n<li><strong>Return Value:<\/strong> The method returns a new <code>Range<\/code> object representing the union of all specified ranges.<\/li>\n<\/ul>\n\n\n\n<p><strong>Code Example<\/strong><br>The following xlwings script demonstrates the use of <code>Application.Union<\/code>. It creates a union of three separate ranges on a sheet and then applies a yellow background fill to all cells within the combined 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\nsheet = wb.sheets&#91;'Sheet1']\n\n# Define three separate, non-adjacent ranges\nrange1 = sheet.range('A1:B2')\nrange2 = sheet.range('D4')\nrange3 = sheet.range('C6:E7')\n\n# Use the Application.Union method via the api property\n# Note: We use .api on the sheet's range objects to get the native Excel Range objects for the Union method.\ncombined_range = app.api.Union(range1.api, range2.api, range3.api)\n\n# Apply formatting to the entire unioned range\ncombined_range.Interior.Color = (255, 255, 0) # Yellow fill\n\n# The action above fills cells A1, A2, B1, B2, D4, and the block C6:E7.<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Application.Union` method in Excel VBA is used to create a single, combined range from two or m&#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-2016","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2016","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=2016"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2016\/revisions"}],"predecessor-version":[{"id":3076,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2016\/revisions\/3076"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}