I have a spreadsheet that is opened via the package openpyxl and I want to apply a specific formula to every cell in row 14 “N” down to the bottom of my current table but make the formula results apply on open of the file because at the moment it populates the cells as required however I have to double click on the cell and hit enter to make the results appear on every row.
# Load the workbook
workbook = load_workbook(new_filepath)
# Select the sheet 'Inc Multiple Classifications'
sheet = workbook['Inc Multiple Classifications']
# Assuming Table14 starts at cell A1 (adjust as per your actual table location)
start_row = 2
start_col = 14
# Determine the dimensions of the table (adjust based on your table's size)
num_rows = lengthofBCCFile # Number of rows in Table14
num_cols = 1 # Number of columns in Table14
# Iterate through each cell in the table and apply the formula
for row in range(start_row, start_row + num_rows):
for col in range(start_col, start_col + num_cols):
cell = sheet.cell(row=row, column=col)
# Build the formula string
formula = f'=TRIM(IFERROR(LET(p, UPPER(SUBSTITUTE(K{row}, " ", "")), n, LEN(p), LEFT(p, n-3)&" "&RIGHT(p, 3)), ""))'
# Set the formula to the cell
cell.value = formula
# Save the workbook
workbook.save(new_filepath)
workbook.close()
This is what the results returned when the file is opened which shows the formula in the cells of column N however no result until I double click on the cell and press enter.