Reputation: 3
I am very new to python, but used excel VBA before. I'm trying to use pywin32 module, as it is using COM to open and read/write excel along with many other reasons..
I have no problem writing formulas in pywin32 to write in excel i.e SUM, divide, minus etc. But I'm having trouble writing functions such as xlUp, CountIF etc in python.
In below code, I am trying to write a code that checks duplicates on all the cells in a column (i.e column "C")
Can you please help on below code?:
from win32com.client import Dispatch
from pathlib import Path
from datetime import datetime
import win32com.client.dynamic
data_folder = Path("C:/..")
file_open = data_folder / "something.xlsx"
xl = Dispatch("Excel.Application")
wb1 = xl.Workbooks.Open(Filename=file_open)
ws1 = wb1.Worksheets(1)
Target = ws1.Range("C3", ws1.Range("C", ws1.Rows.Count).End(xlUp)).Select
for r In Target:
r.Offset(0, 1) = WorksheetFunction.CountIf(Target, r.Value) > 1
excel.Application.quit()
I don't know why it is not working.. how can I translate VBA to Python?
Thank you
Upvotes: 0
Views: 2004
Reputation: 167
To generalize the answer on the last question:
pywin32 code is often very close to a one to one translation of VBA code. But there is a number of small changes needed. Like:
I did not test the code, but the points I pointed out above does prevent your code from running. With mentioned changes I believe it would work. Most VBA code works as expected in pywin32 just adhering to above mentioned rules.
Upvotes: 2