Python脚本没有select在Excelmacrosselect正确的date

我试图在生成每月报告的Excel中运行macros。macros调用python脚本没有任何问题。 然而,无论我在macros中select哪个月份,python脚本总是在一月份运行。

这是我select月份的地方: 这是我选择月份的地方。

当python脚本运行的时候总是一月份: 当python脚本运行的时候总是一月份。

这是python脚本中的代码。

#Read the month and year selected in the Monthly Report pathOfMonthlyReportFile = os.sep.join((os.path.expanduser("~"), 'Desktop'))+'\Monthly Activity Report - Template.xlsm' wb = openpyxl.load_workbook(pathOfMonthlyReportFile, keep_vba=True) sheet = wb.get_sheet_by_name('Macro') MonthOfMonthlyReport = sheet['G10'].value YearOfMonthlyReport = sheet['J10'].value print(MonthOfMonthlyReport) if MonthOfMonthlyReport == "January": MonthNumber = 1 elif MonthOfMonthlyReport == "February": MonthNumber = 2 elif MonthOfMonthlyReport == "March": MonthNumber = 3 elif MonthOfMonthlyReport == "April": MonthNumber = 4 elif MonthOfMonthlyReport == "May": MonthNumber = 5 elif MonthOfMonthlyReport == "June": MonthNumber = 6 elif MonthOfMonthlyReport == "July": MonthNumber = 7 elif MonthOfMonthlyReport == "August": MonthNumber = 8 elif MonthOfMonthlyReport == "September": MonthNumber = 9 elif MonthOfMonthlyReport == "October": MonthNumber = 10 elif MonthOfMonthlyReport == "November": MonthNumber = 11 elif MonthOfMonthlyReport == "December": MonthNumber = 12 MonthlyReportDate = now.replace(month=MonthNumber,year=int(YearOfMonthlyReport)) if MonthNumber == 12: last_day_of_month = MonthlyReportDate.replace(day = 31, month=MonthNumber, year=int(YearOfMonthlyReport)) else: last_day_of_month = MonthlyReportDate.replace(day=1, month=MonthNumber+1, year=int(YearOfMonthlyReport)) - datetime.timedelta(days=1) if MonthNumber >= 10: stringenddate = "%s-%s-%s" % (MonthlyReportDate.year, MonthlyReportDate.month,last_day_of_month.day) stringstartdate = "%s-%s-01" % (MonthlyReportDate.year, MonthlyReportDate.month) else: stringenddate = "%s-0%s-%s" % (MonthlyReportDate.year, MonthlyReportDate.month,last_day_of_month.day) stringstartdate = "%s-0%s-01" % (MonthlyReportDate.year, MonthlyReportDate.month) print(stringenddate) MonthRange = [1,2,3,4,5,6,7,8,9,10,11,12] ThreeMonthsAgo = 0 for i in range(len(MonthRange)): if MonthRange[i] == MonthNumber: ThreeMonthsAgo = MonthRange[i-2] #************** print(ThreeMonthsAgo) if ThreeMonthsAgo<MonthNumber: if ThreeMonthsAgo >=10: stringthreemonthsagodate = "%s-%s-01"%(MonthlyReportDate.year, ThreeMonthsAgo) else: stringthreemonthsagodate = "%s-0%s-01"%(MonthlyReportDate.year, ThreeMonthsAgo) if ThreeMonthsAgo > MonthNumber: if ThreeMonthsAgo >=10: stringthreemonthsagodate = "%s-%s-01"%(MonthlyReportDate.year - 1, ThreeMonthsAgo) else: stringthreemonthsagodate = "%s-0%s-01"%(MonthlyReportDate.year - 1, ThreeMonthsAgo) date_ranges = [(stringstartdate, stringenddate)] data_ranges_for_three_months = [(stringthreemonthsagodate, stringenddate)] 

它应该运行选定的月份,但只运行一月份。 我真的没有得到一个错误消息,但它不是正确的月份。


已经修好了 解决scheme有点尴尬,但我会张贴它。所有我必须做的是每当我在macros中更改月,我需要保存.xlsm,然后运行它。

在Excel电子表格中运行以下代码:

 Sub TestMe() MsgBox Worksheets("Macro").Range("G10") End Sub 

如果你没有得到“三月”,那么你很可能不是参考G10而是其他的东西。 而且,你正在使用启用的VBA通过Python打开excel文件。 你确定,只要你打开它,它不会重置到一January ? 这被称为Event()

它看起来是一个caching/陈旧的数据问题。 如果我正确理解你,你在工作簿中运行一个VBAmacros,调用一个python脚本,然后查看同一个工作簿中的一个单元格? 不是一个理想的结构!

当你的python脚本调用Excel工作表时,在这里:

 wb = openpyxl.load_workbook(pathOfMonthlyReportFile, keep_vba=True) 

我认为这是看你上次保存文件时G10的价值。 也许这个月是最后一次保存的一月份?

我会:

  1. 重新调整你的python函数,以便在你调用VBA中的函数时,它需要月份和date参数并传递这些值

  2. 尝试让VBA在VBA调用python脚本之前保存表单,从而保留对月份和date下拉列表所做的任何更改。

请注意,1.比2.更强大,更专业,但可能稍微复杂一些。