{"id":2172,"date":"2026-07-08T16:00:14","date_gmt":"2026-07-08T08:00:14","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2172"},"modified":"2026-03-28T09:33:46","modified_gmt":"2026-03-28T09:33:46","slug":"how-to-use-applicationreferencestyle-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationreferencestyle-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.ReferenceStyle in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Application.ReferenceStyle property in Excel VBA determines the style of cell references used in formulas\u2014either A1-style (the default) or R1C1-style. In xlwings, this property can be accessed and modified via the <code>api<\/code> property of an Application object, allowing Python scripts to control reference styles programmatically. This is particularly useful when generating or evaluating formulas dynamically, as different styles may be preferred for readability or compatibility with other systems.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The ReferenceStyle property specifies whether Excel uses A1-style references (e.g., &#8220;A1&#8221; for the top-left cell) or R1C1-style references (e.g., &#8220;R1C1&#8221; for the same cell). A1-style is common in everyday use, while R1C1-style can simplify formula creation in macros by using relative row and column numbers. Changing this setting affects all formulas in the workbook, influencing how they are displayed and interpreted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, the property is accessed through the Application object&#8217;s <code>api<\/code> attribute:<br><code>app.api.ReferenceStyle<\/code><br>This property can be both read and written. It accepts integer values corresponding to Excel constants:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>xlA1<\/code> (or <code>1<\/code>): Sets references to A1-style.<\/li>\n\n\n\n<li><code>xlR1C1<\/code> (or <code>-4150<\/code>): Sets references to R1C1-style.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To use these constants in xlwings, import them from the <code>xlwings.constants<\/code> module or use their numeric equivalents. For example:<br><code>from xlwings.constants import ReferenceStyle<\/code><br>Then, <code>ReferenceStyle.xlA1<\/code> or <code>ReferenceStyle.xlR1C1<\/code> can be used.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><br>Here are practical xlwings API instances demonstrating the usage of ReferenceStyle:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Check the Current Reference Style<\/strong><br>This code retrieves and prints the current reference style of the Excel application.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\ncurrent_style = app.api.ReferenceStyle\nprint(f\"Current reference style: {current_style}\") # Outputs 1 for xlA1 or -4150 for xlR1C1<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Switch to R1C1 Reference Style<\/strong><br>This example changes the reference style to R1C1, which can be useful for formula manipulation in VBA-like scripts.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nfrom xlwings.constants import ReferenceStyle\napp = xw.apps.active\napp.api.ReferenceStyle = ReferenceStyle.xlR1C1\n# Verify the change\nprint(f\"Updated to: {app.api.ReferenceStyle}\") # Should show -4150<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Use ReferenceStyle in Formula Creation<\/strong><br>After setting the style, formulas entered will follow the chosen format. This code adds a formula to a cell based on the current reference style.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.apps.active\nwb = app.books.active\nsheet = wb.sheets&#91;0]\n# Ensure A1 style for clarity\napp.api.ReferenceStyle = 1 # xlA1\nsheet.range(\"B2\").formula = \"=SUM(A1:A10)\"\n# Switch to R1C1 and add another formula\napp.api.ReferenceStyle = -4150 # xlR1C1\nsheet.range(\"B3\").formula = \"=SUM(R&#91;-2]C&#91;-1]:R&#91;7]C&#91;-1])\" # Equivalent to A1:A10 from B3's perspective<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Application.ReferenceStyle property in Excel VBA determines the style of cell references used in&#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-2172","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2172","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=2172"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2172\/revisions"}],"predecessor-version":[{"id":3321,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2172\/revisions\/3321"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}