Python脚本未获取在Excel宏

时间:2017-12-06 16:13:03

标签: python excel vba excel-vba

我正在尝试在excel中运行一个生成月度报告的宏。宏调用python脚本没有任何问题。但是无论我在宏中选择哪个月,python脚本总是运行一月。

这是我选择月份的地方: This is where i select the month.

当python脚本运行时,它始终是1月: When the python script runs it is always January.

这是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然后运行它。

2 个答案:

答案 0 :(得分:0)

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

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

如果你没有得到“March”,那么你很可能不会引用G10而是引用其他内容。 此外,您通过启用了VBA的Python打开excel文件。您确定,无论何时打开它,它都不会重置回January?这称为Event()

答案 1 :(得分:0)

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

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

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

我认为您上次保存文件时会查看G10的值。也许这个月是最后一次保存的1月?

我要么:

  1. 重构你的python函数,以便它需要月份和日期参数,并在从VBA调用函数时传递这些值

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

  3. 请注意,1.比2更健壮,更专业,但实施起来可能稍微复杂一些。