{"id":2307,"date":"2026-09-14T07:26:03","date_gmt":"2026-09-13T23:26:03","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2307"},"modified":"2026-03-28T12:47:55","modified_gmt":"2026-03-28T12:47:55","slug":"how-to-use-worksheetcodename-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetcodename-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.CodeName in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>CodeName<\/code> property of a Worksheet object in Excel is a powerful feature that allows developers to assign a unique, programmatic identifier to a sheet. Unlike the <code>Name<\/code> property, which is the visible tab name a user can change, the <code>CodeName<\/code> is intended to remain static through the life of the workbook. This makes it an ideal reference in VBA macros and, by extension, in xlwings scripts, as it provides a stable way to target a specific sheet even if a user renames its tab. In xlwings, you access this property directly through the <code>api<\/code> object, which grants raw access to the underlying Excel object model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax for accessing the <code>CodeName<\/code> property in xlwings is straightforward. Since it is a read-only property in the context of xlwings (typically set in the Excel VBA IDE), you primarily retrieve its value. The call format is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sheet.api.CodeName<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>sheet<\/code> is an xlwings <code>Sheet<\/code> object. The property returns a string representing the sheet&#8217;s programmatic name. It&#8217;s important to note that while you can <em>read<\/em> the <code>CodeName<\/code> via xlwings, changing it programmatically is not directly supported through the standard xlwings API; it is generally set in the Visual Basic for Applications editor (by changing the <code>(Name)<\/code> property in the Properties window for the sheet) before or during development.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a workbook where you have a data input sheet. In the VBA IDE, you set its <code>CodeName<\/code> to <code>shDataInput<\/code>. Even if a user later changes the tab name from &#8220;Data&#8221; to &#8220;Monthly Data&#8221;, your xlwings code can still reliably find it. Here is a practical example:<\/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\n\n# Method 1: Get a sheet by its CodeName by iterating\ntarget_code_name = \"shDataInput\"\ntarget_sheet = None\nfor sheet in wb.sheets:\n    if sheet.api.CodeName == target_code_name:\n        target_sheet = sheet\n        break\n\nif target_sheet:\n    print(f\"Found sheet with CodeName: {target_sheet.api.CodeName}\")\n    # Now you can work with the sheet reliably\n    target_sheet.range(\"A1\").value = \"Updated via CodeName\"\nelse:\n    print(\"Sheet not found.\")\n\n# Method 2: A more direct approach using the `api` collection (requires knowing the index\/name in the VBA project)\n# This is less common but demonstrates the direct COM access.\ntry:\n    # The VBA workbook object has a `Worksheets` collection accessible via `api`\n    vba_sheet = wb.api.Worksheets(target_code_name) # This uses the *CodeName* in    the VBA collection\n    xw_sheet = xw.Sheet(vba_sheet)\n    print(f\"Direct access successful. Sheet name (tab): {xw_sheet.name}\")\nexcept Exception as e:\n    print(f\"Direct access failed: {e}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `CodeName` property of a Worksheet object in Excel is a powerful feature that allows developers &#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-2307","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2307","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=2307"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2307\/revisions"}],"predecessor-version":[{"id":3518,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2307\/revisions\/3518"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}