excel中closures工作簿,每周更改一次
我有一个与下面的macros的文件。 基本上我需要它closures所有打开的工作簿,但是我的文件名每周更改一次,例如本周它被称为特殊服务1503,下周它将是特殊服务1504等。关于如何编辑的任何想法下面的代码,以便我可以closures此文件,而无需手动编辑vba中的数字?
Workbooks("2014 Actuals").Close SaveChanges = True Workbooks("Special Services Budget 2015").Close SaveChanges = True
上面的工作,因为没有编辑,下面的工作,但我手动必须每周更改周数从1504到1505等。
Workbooks("Special Services 1504").Close SaveChanges = True
任何想法家伙?
我将这个作为一个单独的答案join,因为它与前一个完全不同:
当且仅当您确定只有该工作簿具有特殊服务1054的名称时,才可以使用Like运算符 :
For Each objWb In Workbooks If objWb.Name Like "Special Services *" objWb.Close SaveChanges = True End If Next objWb
正如我以前的回答中所述,这种方法比另一种方法更可靠, 但前提是您100%确定可能没有打开类似名称的其他工作簿 。
如果我理解逻辑,你只需要build立一个dynamic索引:
firstWeek2015 = 1500 '<-- starting seed yearFactor = Year(Now()) - 2015 '<-- we take the current year and we subtract 2015: this year it will be 0, next year 1 etc. weekFactor = WorksheetFunction.WeekNum(Now()) '<--we just take the current week newIndex = firstWeek2015 + yearFactor*52 + weekFactor
并将其replace为您的工作簿名称:
Workbooks("Special Services " & newIndex).Close SaveChanges = True
即使我宁愿:
1) Use the same logic that you used to open the file; 2) Use a RegEx-based solution (if "Special Services ????" is the only file of that kind).