{"id":2403,"date":"2026-11-01T07:13:10","date_gmt":"2026-10-31T23:13:10","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2403"},"modified":"2026-03-29T10:20:12","modified_gmt":"2026-03-29T10:20:12","slug":"how-to-use-rangefindprevious-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangefindprevious-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.FindPrevious in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>FindPrevious<\/code> method in the <code>Range<\/code> object is a powerful tool for searching backward through a worksheet to locate specific data, building upon the criteria set by a prior <code>Find<\/code> operation. In xlwings, this functionality is accessed via the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This method is particularly useful when you need to find the previous occurrence of a value or format, continuing a search loop in reverse order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br><code>FindPrevious<\/code> continues a search that was initiated with the <code>Find<\/code> method. It searches backward (upward and to the left) from the current cell to find the next cell matching the same search criteria. This is essential for iterative searches, allowing you to cycle through all matches in reverse. If no match is found, it returns <code>None<\/code>. The search parameters, such as <code>What<\/code>, <code>LookIn<\/code>, <code>LookAt<\/code>, <code>SearchOrder<\/code>, and <code>SearchDirection<\/code>, are inherited from the previous <code>Find<\/code> call. In xlwings, you typically use this within a loop to process multiple instances.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>The xlwings API call follows the pattern:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cell = range.api.FindPrevious(After)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>After<\/code>: This is a required parameter. It specifies the cell <em>after<\/em> which the search should begin looking backward. Typically, you pass the cell from which you want to start searching in reverse. In practice, you often use the cell found by the previous <code>Find<\/code> or <code>FindPrevious<\/code> call as the <code>After<\/code> argument to continue the search. If omitted, the search starts from the top-left cell of the range.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><br>Suppose you have a list of sales figures in column A, and you want to find all occurrences of the value &#8220;1000&#8221; starting from the bottom. The following xlwings code demonstrates using <code>FindPrevious<\/code> in a loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active workbook and sheet\nwb = xw.books.active\nws = wb.sheets&#91;'Sheet1']\n\n# Define the search range (e.g., entire column A)\nsearch_range = ws.range('A:A')\n\n# Perform the initial Find to set criteria and get the last match\nfirst_cell = search_range.api.Find(What=\"1000\",\nAfter=search_range.api.Cells(search_range.api.Rows.Count, 1),\nLookIn=-4163, # xlValues\nLookAt=1) # xlWhole\n\nif first_cell is not None:\n    found_cell = first_cell\n    print(f\"Found at: {found_cell.Address}\")\n\n# Use FindPrevious to find earlier occurrences\nwhile True:\n    # Search backward from the current found cell\n    next_cell = search_range.api.FindPrevious(After=found_cell)\n\n# Check if we found a new cell and it's not the same as the first one to avoid infinite loop\nif next_cell is None or next_cell.Address == first_cell.Address:\n    break\n\n    found_cell = next_cell\n    print(f\"Found at: {found_cell.Address}\")\nelse:\n    print(\"Value not found.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `FindPrevious` method in the `Range` object is a powerful tool for searching backward through a &#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-2403","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2403","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=2403"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2403\/revisions"}],"predecessor-version":[{"id":3656,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2403\/revisions\/3656"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}