我正在尝试在生成月度报告的 excel 中运行一个宏。宏调用 python 脚本没有任何问题。然而,无论我在宏中选择哪个月份,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)]
它应该在所选月份运行,但仅在 1 月份运行。
我并没有真正收到错误消息,但这不是正确的月份。
它已被修复。解决方案有点尴尬,但我会发布它。我所要做的就是每当我在宏中更改月份时,我需要保存 .xlsm 然后运行它。
最佳答案
在 Excel 电子表格中运行此代码:
Sub TestMe()
MsgBox Worksheets("Macro").Range("G10")
End Sub
如果你没有得到“March”,那么你很可能没有引用
G10
但对于别的东西。此外,您正在通过
Python
打开 excel 文件。启用 VBA。您确定吗,无论何时打开它,它都不会重置为 January
?这称为 Event()
.
关于Python脚本没有选择在excel宏中选择的正确日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47678775/