{"id":2162,"date":"2026-07-03T16:06:42","date_gmt":"2026-07-03T08:06:42","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2162"},"modified":"2026-03-28T09:15:03","modified_gmt":"2026-03-28T09:15:03","slug":"how-to-use-applicationpreviousselections-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationpreviousselections-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.PreviousSelections in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>PreviousSelections<\/code> property of the <code>Application<\/code> object in Excel is a powerful feature for tracking and managing the last four cells or ranges that were selected by the user or set via VBA. In xlwings, this functionality is accessible through the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This property is read-only and returns an array of <code>Range<\/code> objects, making it useful for scenarios where you need to revert to previous selections, analyze user navigation patterns, or create custom undo features for specific actions within a workbook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The primary function of <code>PreviousSelections<\/code> is to store a history of the most recent selections in the Excel application. Each time a new cell or range is selected, the list updates, with the oldest entry being removed when the history exceeds four items. This is particularly valuable in complex macros or automated processes where you might need to reference or return to a prior location after performing intermediate operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, you access this property via the <code>Application<\/code> object. The syntax is straightforward:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\nprevious_selections = app.api.PreviousSelections<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>app.api<\/code> bridges to Excel&#8217;s COM interface. <code>PreviousSelections<\/code> returns a one-based array (index starting at 1) of <code>Range<\/code> objects. You can iterate through this array or access individual items by index. Note that if there are fewer than four previous selections, the array will contain only the available items, and attempting to access an index beyond the count may result in an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><br>Below are practical examples demonstrating how to use <code>PreviousSelections<\/code> with xlwings:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Listing All Previous Selections:<\/strong><br>This code prints the address of each stored range. It checks the count of items to avoid errors.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\nselections = app.api.PreviousSelections\nfor i in range(1, selections.Count + 1):\n    range_obj = selections(i)\n    print(f\"Selection {i}: {range_obj.Address}\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Accessing the Most Recent Previous Selection:<\/strong><br>To get the immediate previous selection (the second item in the list, as the first is the current selection), you can index directly. Note that indexing starts at 1.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\n# Get the most recent previous selection (index 2 if current is index 1)\nrecent_selection = app.api.PreviousSelections(2)\nif recent_selection:\n    print(f\"Recent previous selection: {recent_selection.Address}\")<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Using PreviousSelections in a Macro-Like Function:<\/strong><br>This example saves the current selection, performs an operation, and then returns to the saved selection using the history.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\ndef example_previous_selections():\napp = xw.apps.active\n# Assume user has selected a range before running this\noriginal_range = app.api.PreviousSelections(1) # Current selection\n# Perform some operation, e.g., select another range\napp.range(\"A10\").select()\n# Now, revert to the original selection using PreviousSelections\n# Since the selection changed, original_range is now in index 2\nreverted_range = app.api.PreviousSelections(2)\nreverted_range.select()\nprint(f\"Reverted to: {reverted_range.Address}\")\nexample_previous_selections()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `PreviousSelections` property of the `Application` object in Excel is a powerful feature for tra&#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-2162","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2162","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=2162"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2162\/revisions"}],"predecessor-version":[{"id":3306,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2162\/revisions\/3306"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}