{"id":2106,"date":"2026-06-05T15:38:47","date_gmt":"2026-06-05T07:38:47","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2106"},"modified":"2026-03-28T07:06:20","modified_gmt":"2026-03-28T07:06:20","slug":"how-to-use-applicationexcel4macrosheets-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationexcel4macrosheets-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.Excel4MacroSheets in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Excel4MacroSheets<\/code> property of the Application object in Excel&#8217;s object model is a legacy feature primarily used for backward compatibility with Excel version 4.0 macros. It returns a <code>Sheets<\/code> collection that contains all the Excel 4.0 macro sheets in a workbook. These macro sheets are a predecessor to VBA modules and are rarely used in modern Excel development. However, when working with older workbooks or specific automation tasks that involve these sheets, accessing them via xlwings can be necessary.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The <code>Excel4MacroSheets<\/code> property allows you to programmatically access and manipulate Excel 4.0 macro sheets within a workbook. This can include tasks such as counting the number of such sheets, iterating through them, reading or writing data, or checking for their existence. In xlwings, this property is exposed through the Application object, enabling integration with Python scripts for automation and data processing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, the property is accessed via the <code>api<\/code> property of an App or Workbook object, which provides direct access to the underlying Excel object model. The syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app.api.Excel4MacroSheets<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This returns a collection object representing all Excel 4.0 macro sheets in the active or specified workbook. The collection can be indexed or iterated over. Note that this property does not take any parameters, as it is a read-only property that retrieves the collection based on the current workbook context.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameters and Usage:<\/strong><br>The <code>Excel4MacroSheets<\/code> property has no parameters. It is called directly from the Application object. To use it effectively, ensure that the workbook contains Excel 4.0 macro sheets; otherwise, the collection may be empty. The returned collection supports typical methods and properties, such as <code>Count<\/code> to get the number of sheets or <code>Item(index)<\/code> to access a specific sheet by index or name. Here\u2019s a brief overview of common operations:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operation<\/th><th>xlwings API Example<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>Get count<\/td><td><code>app.api.Excel4MacroSheets.Count<\/code><\/td><td>Returns the number of Excel 4.0 macro sheets.<\/td><\/tr><tr><td>Access by index<\/td><td><code>app.api.Excel4MacroSheets(1)<\/code><\/td><td>Retrieves the first macro sheet (1-based index).<\/td><\/tr><tr><td>Iterate through sheets<\/td><td>Loop over <code>app.api.Excel4MacroSheets<\/code><\/td><td>Processes each macro sheet in the collection.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><br>Below are practical examples using xlwings to work with the <code>Excel4MacroSheets<\/code> property. These assume you have an instance of the Excel application and a workbook open.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Counting Excel 4.0 Macro Sheets:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active # Get the active Excel application\nmacro_sheets_count = app.api.Excel4MacroSheets.Count\nprint(f\"Number of Excel 4.0 macro sheets: {macro_sheets_count}\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Listing All Excel 4.0 Macro Sheet Names:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\nmacro_sheets = app.api.Excel4MacroSheets\nfor sheet in macro_sheets:\n    print(sheet.Name) # Print the name of each macro sheet<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Accessing a Specific Macro Sheet and Reading Data:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\n# Access the first Excel 4.0 macro sheet by index\nmacro_sheet = app.api.Excel4MacroSheets(1)\n# Read a value from cell A1 on the macro sheet\ncell_value = macro_sheet.Range(\"A1\").Value\nprint(f\"Value in A1: {cell_value}\")<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Checking for Existence and Adding Data:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\nif app.api.Excel4MacroSheets.Count > 0:\n    macro_sheet = app.api.Excel4MacroSheets(1)\n    macro_sheet.Range(\"B2\").Value = \"Updated via xlwings\"\n    print(\"Data written to macro sheet.\")\nelse:\n    print(\"No Excel 4.0 macro sheets found.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Excel4MacroSheets` property of the Application object in Excel&apos;s object model is a legacy featu&#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-2106","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2106","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=2106"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2106\/revisions"}],"predecessor-version":[{"id":3223,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2106\/revisions\/3223"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}