How to use Range.FillUp in the xlwings API way

The FillUp method in Excel’s Range object is a powerful tool for data manipulation, allowing users to propagate values, formulas, or formatting from the bottom of a specified range upward to fill empty cells. This is particularly useful for completing datasets where information is missing in the upper cells but present in the lower ones, essentially performing a reverse fill operation compared to the more common FillDown. In xlwings, this functionality is accessed through the api property, which provides direct access to the underlying Excel object model, enabling precise and programmatic control over workbook operations.

Functionality
The primary function of the FillUp method is to copy the content and formatting from the last cell(s) in a range into the preceding empty cells within that same range. It operates on a Range object, which can represent a single cell, a row, a column, or a multi-cell block. This method is invaluable for cleaning and preparing data, especially when dealing with reports where summary or total values at the bottom need to be applied to empty detail rows above.

Syntax in xlwings
In xlwings, you invoke this method via the Excel VBA object model interface. The syntax is:

range.api.FillUp()

Here, range is an xlwings Range object. The FillUp method does not take any parameters. It acts directly on the range it is called upon. The key aspect is the definition of the range: the bottom-most cell(s) of this range must contain the data you want to propagate upward. The method will fill all cells above within the range until it encounters a non-empty cell or reaches the top of the range.

Code Example
Consider a scenario where you have a column of data in Excel, but only the last cell (the total) has a value or formula, and you need to fill that upward to all empty cells in the column.

import xlwings as xw

# Connect to the active Excel workbook and sheet
app = xw.apps.active
wb = app.books.active
sheet = wb.sheets['Sheet1']

# Define the target range. For example, column A from row 1 to row 10.
# Assume cell A10 contains the value "Total" or a formula like "=SUM(A1:A9)", and A1:A9 are empty.
target_range = sheet.range('A1:A10')

# Apply the FillUp method
target_range.api.FillUp()

# Now, the value or formula from A10 is copied into cells A1 through A9.

Another practical example involves filling a formula up a column to perform calculations on preceding rows:

import xlwings as xw

# Start Excel instance and open workbook
with xw.App(visible=False) as app:
wb = app.books.open('data_workbook.xlsx')
sheet = wb.sheets[0]

# Suppose we want a 10% increase calculation. We put the formula in the bottom cell.
# Cell B10 contains the formula: =A10*1.1
sheet.range('B10').formula = '=A10*1.1'

# We want to apply this same formula to cells B1 through B9.
fill_range = sheet.range('B1:B10')
fill_range.api.FillUp()

# After execution, cells B1:B9 will contain formulas like =A1*1.1, =A2*1.1, etc.
wb.save()
wb.close()

October 30, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *