Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write VLOOKUP to each row in a column – openpyxl

Trying to apply a vlookup formula to each row in a given column (‘B’) with the code below;

import openpyxl

path = 'test.xlsx'
wb = openpyxl.load_workbook(path)
ws2 = wb['Sheet2']

for eachrow in ws2['B']:
    print(eachrow)
    openpyxl.cell.value = '=VLOOKUP(B{0},Sheet1!A:A,1,0)'

print(ws2['B2'].value)

the print(eachrow) line gives me all the rows individually as I want them but the next line does not update the values with the provided VLOOKUP string.

print(ws2['B2'].value) returns the value None

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This line openpyxl.cell.value = '=VLOOKUP(B{0},Sheet1!A:A,1,0)' is not doing what you think it’s doing. You already have a reference to the cell; eachrow. Instead, use:

eachrow.value = '=VLOOKUP(B{0},Sheet1!A:A,1,0)'

However, openpyxl does not evaluate excel formulas, so your call to print will only print =VLOOKUP(B{0},Sheet1!A:A,1,0)'. If you want to evaluate the formula you could look at using xlwings

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading