Mar-05-2024, 06:49 PM
(This post was last modified: Mar-05-2024, 06:49 PM by MasterOfDestr.)
(Mar-04-2024, 10:23 AM)Aften1961 Wrote: Did you solve the problem?
Partially. I might use translate as below. But to be honest I more willing of creating VBA and launch it using Python.
Translate solution seems be sensitive for what kind of data are copied/included in Excel. Using "translate" solution not always cooperate with my files as for my report I use data from different sources. With VBA I don't have such issue.
#Version 3 from openpyxl import load_workbook from openpyxl.formula.translate import Translator # Load the workbook wb = load_workbook('_MAT_TEMPLATE_Python.xlsx') ws = wb["Material prices"] # Get the total number of rows based on column A total_rows = sum(1 for row in ws.iter_rows(max_col=1, max_row=ws.max_row, values_only=True) if row[0]) # Generate the formula for column B orig_formula = "=VLOOKUP($A7,'Master data'!$A:$H,2,0)" orig_cell = 'B7' ws[orig_cell] = orig_formula # Apply formulas in column B for row in range(8, total_rows+1): dst_cell = f"B{row}" # Apply the formula to the cell translating from cell B7 ws[dst_cell] = Translator(orig_formula, origin=orig_cell).translate_formula(dst_cell) # Save the workbook wb.save("_MAT_TEMPLATE_Python.xlsx")