{"id":2015,"date":"2026-04-21T07:56:00","date_gmt":"2026-04-20T23:56:00","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2015"},"modified":"2026-03-28T04:54:59","modified_gmt":"2026-03-28T04:54:59","slug":"how-to-use-applicationundo-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationundo-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.Undo in the xlwings API way"},"content":{"rendered":"\n<p>The Application.Undo method in Excel&#8217;s object model provides a way to reverse the last user-interface action performed in Excel, such as typing in a cell, formatting, or deleting data. In xlwings, this functionality is exposed through the <code>api<\/code> property, which grants direct access to the underlying Excel COM object. This allows Python scripts to mimic the &#8220;Undo&#8221; command typically executed by pressing Ctrl+Z, offering a mechanism to revert unintended changes programmatically. It is important to note that the Undo method is primarily designed for actions initiated through the Excel interface and may not work for changes made via VBA or COM automation in certain contexts. However, when called immediately after a user-style action performed via xlwings (like writing a value via the Excel interface), it can be effective.<\/p>\n\n\n\n<p>The syntax for invoking the Undo method in xlwings is straightforward, as it does not take any parameters. The call is made through the Application object accessed from an xlwings <code>App<\/code> or <code>Book<\/code> instance. The general format is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.Undo()<\/code><\/pre>\n\n\n\n<p>Here, <code>app<\/code> refers to the xlwings <code>App<\/code> object representing the Excel application instance. The <code>api<\/code> property provides the native Excel Application COM object, and <code>Undo()<\/code> is the method call. No arguments are required or accepted. The method will reverse the last action if an undo history is available; otherwise, it may have no effect or raise an error in some scenarios.<\/p>\n\n\n\n<p>For example, consider a scenario where a user manually types a value into a cell in an open Excel workbook, and then a script needs to undo that action. The following xlwings code demonstrates this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance\napp = xw.apps.active\n\n# Assume a user just typed \"Test\" into cell A1 of the active sheet manually\n# To undo that entry programmatically:\napp.api.Undo()\n\n# This will revert the change in cell A1, restoring its previous value or clearing it if it was empty.<\/code><\/pre>\n\n\n\n<p>Another example involves performing an action through xlwings that mimics user interaction, followed by an undo. Note that not all xlwings operations populate the undo stack, as many bypass the UI. However, using <code>Range.value<\/code> setter might be treated as a user action in some contexts. A more reliable approach is to simulate keystrokes or use <code>SendKeys<\/code>, but a simpler method is to leverage Excel&#8217;s <code>Application.Run<\/code> to execute a macro that performs the action, which can then be undone. Below is an illustrative code snippet that writes a value using the Excel interface via <code>Application.Run<\/code> and then undoes it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\napp = xw.apps.active\nwb = app.books.active\nsheet = wb.sheets&#91;0]\n\n# Use Application.Run to execute a VBA-like operation that can be undone\n# First, define a simple VBA function in a module (this requires VBA access; alternatively, use a pre-existing macro)\n# For demonstration, assume a macro named \"WriteValue\" exists that writes to cell B2.\n# Since xlwings can run macros, we can call it and then undo.\nwb.api.Run(\"WriteValue\") # This macro might set cell B2 to \"Hello\"\napp.api.Undo() # This should undo the macro's action, reverting cell B2<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Application.Undo method in Excel&apos;s object model provides a way to reverse the last user-interfac&#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-2015","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2015","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=2015"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2015\/revisions"}],"predecessor-version":[{"id":3075,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2015\/revisions\/3075"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}