{"id":2402,"date":"2026-10-31T16:47:00","date_gmt":"2026-10-31T08:47:00","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2402"},"modified":"2026-03-29T10:19:18","modified_gmt":"2026-03-29T10:19:18","slug":"how-to-use-rangefindnext-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangefindnext-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.FindNext in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>FindNext<\/code> member of the <code>Range<\/code> object in Excel is a method used to continue a search initiated by the <code>Find<\/code> method. It locates the next cell that matches the same criteria specified in the original <code>Find<\/code> call, effectively allowing you to iterate through all matching cells in a worksheet. This is particularly useful for tasks like finding all occurrences of a specific value, format, or formula within a range. The search proceeds in the specified direction (by default, down the worksheet) and wraps around to the beginning if needed, depending on the parameters set in the initial <code>Find<\/code> operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, the <code>FindNext<\/code> method is accessed through the <code>api<\/code> property of a <code>Range<\/code> object, which provides access to the underlying Excel object model. The syntax for calling <code>FindNext<\/code> via xlwings is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_object.api.FindNext(After)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>After<\/strong>: This is an optional parameter that specifies the cell after which the search should start. It must be a single <code>Range<\/code> object, typically the cell found in the previous search. If omitted, the search starts after the top-left cell in the range, which is the default behavior in Excel. In practice, you often pass the last found cell to ensure the search continues correctly without missing matches or entering an infinite loop.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The method returns a <code>Range<\/code> object representing the next cell that meets the criteria, or <code>None<\/code> if no further matches are found. It&#8217;s important to note that <code>FindNext<\/code> relies on the search settings (like <code>What<\/code>, <code>LookIn<\/code>, <code>LookAt<\/code>, etc.) from the previous <code>Find<\/code> call, so it should always be used in conjunction with <code>Find<\/code> to define those criteria first.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a code example that demonstrates using <code>FindNext<\/code> with xlwings to find all occurrences of the value &#8220;Data&#8221; in a worksheet and print their addresses:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel workbook and sheet\nwb = xw.books.active\nws = wb.sheets.active\n\n# Define the search range and criteria using Find\nsearch_range = ws.used_range\nfirst_cell = search_range.api.Find(What=\"Data\", LookIn=-4163) # -4163 corresponds to xlValues\n\n# Check if any match was found\nif first_cell:\n    print(f\"First match at: {first_cell.Address}\")\n\n# Store the first match address to detect when we've looped back to it\nfirst_address = first_cell.Address\n\n# Use FindNext to find subsequent matches\nnext_cell = first_cell\nwhile True:\n    next_cell = next_cell.api.FindNext(After=next_cell)\n    if not next_cell or next_cell.Address == first_address:\n        break # Exit if no more matches or looped back to start\n        print(f\"Next match at: {next_cell.Address}\")\n    else:\n        print(\"No matches found.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `FindNext` member of the `Range` object in Excel is a method used to continue a search initiated&#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-2402","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2402","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=2402"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2402\/revisions"}],"predecessor-version":[{"id":3654,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2402\/revisions\/3654"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}