How to use Application.PreviousSelections in the xlwings API way

The PreviousSelections property of the Application 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 api property, which provides direct access to the underlying Excel object model. This property is read-only and returns an array of Range 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.

Functionality
The primary function of PreviousSelections 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.

Syntax
In xlwings, you access this property via the Application object. The syntax is straightforward:

import xlwings as xw
app = xw.apps.active
previous_selections = app.api.PreviousSelections

Here, app.api bridges to Excel’s COM interface. PreviousSelections returns a one-based array (index starting at 1) of Range 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.

Code Examples
Below are practical examples demonstrating how to use PreviousSelections with xlwings:

  1. Listing All Previous Selections:
    This code prints the address of each stored range. It checks the count of items to avoid errors.
import xlwings as xw
app = xw.apps.active
selections = app.api.PreviousSelections
for i in range(1, selections.Count + 1):
    range_obj = selections(i)
    print(f"Selection {i}: {range_obj.Address}")
  1. Accessing the Most Recent Previous Selection:
    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.
import xlwings as xw
app = xw.apps.active
# Get the most recent previous selection (index 2 if current is index 1)
recent_selection = app.api.PreviousSelections(2)
if recent_selection:
    print(f"Recent previous selection: {recent_selection.Address}")
  1. Using PreviousSelections in a Macro-Like Function:
    This example saves the current selection, performs an operation, and then returns to the saved selection using the history.
import xlwings as xw
def example_previous_selections():
app = xw.apps.active
# Assume user has selected a range before running this
original_range = app.api.PreviousSelections(1) # Current selection
# Perform some operation, e.g., select another range
app.range("A10").select()
# Now, revert to the original selection using PreviousSelections
# Since the selection changed, original_range is now in index 2
reverted_range = app.api.PreviousSelections(2)
reverted_range.select()
print(f"Reverted to: {reverted_range.Address}")
example_previous_selections()

July 3, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *