{"id":2393,"date":"2026-10-27T07:17:30","date_gmt":"2026-10-26T23:17:30","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2393"},"modified":"2026-03-29T10:13:30","modified_gmt":"2026-03-29T10:13:30","slug":"how-to-use-rangedirty-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangedirty-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.Dirty in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Dirty<\/code> member of the <code>Range<\/code> object in Excel&#8217;s object model is a method used to mark a specific range of cells as &#8220;dirty,&#8221; meaning they have been modified or edited. This is particularly useful in scenarios involving calculation dependencies or when you need to force a recalculation for specific cells without recalculating the entire workbook. In xlwings, the <code>Dirty<\/code> method can be accessed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This allows for precise control over calculation triggers in automated Excel tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The primary function of the <code>Dirty<\/code> method is to flag a range as changed, prompting Excel to recalculate any formulas that depend on the cells within that range during the next calculation cycle. This can improve performance by avoiding full workbook recalculations and ensuring that only relevant parts of the spreadsheet are updated. It is especially beneficial in large workbooks with complex formulas or data models where selective recalculation is needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, the <code>Dirty<\/code> method is called using the <code>api<\/code> property on a <code>Range<\/code> object. The syntax is as follows:<br><code>range.api.Dirty()<\/code><br>Here, <code>range<\/code> refers to an xlwings <code>Range<\/code> object representing the cells you want to mark as dirty. The method does not take any parameters. It directly applies to the specified range, marking all cells within it as modified.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><br>Consider a scenario where you have an Excel workbook with formulas in column B that depend on values in column A. After updating some cells in column A programmatically using xlwings, you can use the <code>Dirty<\/code> method to ensure that the dependent formulas in column B are recalculated. Below is a code example demonstrating this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active workbook\nwb = xw.books.active\nsheet = wb.sheets&#91;'Sheet1']\n\n# Update values in column A (e.g., cells A1:A5)\nfor i in range(1, 6):\n    sheet.range(f'A{i}').value = i * 10 # Set values to 10, 20, ..., 50\n\n# Mark the updated range as dirty to trigger recalculation of dependent formulas\nsheet.range('A1:A5').api.Dirty()\n\n# Optional: Force a calculation if automatic calculation is disabled\nwb.app.calculate()\n\n# Read the results from column B to verify recalculation\nfor i in range(1, 6):\n    result = sheet.range(f'B{i}').value\n    print(f'Cell B{i} value after dirty mark: {result}')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Dirty` member of the `Range` object in Excel&apos;s object model is a method used to mark a specific&#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-2393","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2393","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=2393"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2393\/revisions"}],"predecessor-version":[{"id":3641,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2393\/revisions\/3641"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}