Home > Mobile >  Filling a column with formula using xlwings
Filling a column with formula using xlwings

Time:01-13

I am trying to write code for coping values from on column to another column using excel formula with xlwings

Sample code

If I am using above code it is coping the cell value of V3 to B3.

expected code or result

But I want to copy values of each cells of V to B make iterrows like below

But it was not working.

Can anyone help me with this?

CodePudding user response:

The problem is that you need to use an f-string to format the text:

for i in range(1, n 1):
    xw.Range(f"B{i}").formula = f"=V{i}"

A much more efficient way to do the above is setting a range of cells to the formula of the first cell, as the rest will be relative without absolute referencing:

n = 10
sheet.range("B1:B" str(n)).formula = "=V1"

So by using the above, cells B1:B10 will have have the values of the cells V1:V10.

I would also suggest using sheet.range() instead of xw.Range() as this will ensure the correct sheet is being edited.

  • Related