Excel 2013 VBA显示4位数年份,2位数周

时间:2016-04-12 11:38:56

标签: excel vba excel-vba

Goodafternoon all,

目前,我正在尝试填充第一行中的以下表格,这是VLOOKUP所需的。我想将此行定义为"yyyy-ww",因此本周"2016-15"。这样可以正常工作,但是当它在一年中较早时,周数将仅为1位数,"2016-1"而不是预期的"2016-01"。不幸的是格式化不起作用。

有人可以帮帮我吗?

公式基本上是将year, "-", current week放在一起,然后转移到12周减去(所以,我们现在在第15周,我希望显示第03到第15周)。如何以正确的格式显示周?

 Range("A4").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-12)"
    Range("A5").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-11)"
    Range("A6").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-10)"
    Range("A7").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-9)"
    Range("A8").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-8)"
    Range("A9").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-7)"
    Range("A10").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-6)"
    Range("A11").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-5)"
    Range("A12").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-4)"
    Range("A13").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-3)"
    Range("A14").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-2)"
    Range("A15").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-1)"
    Range("A16").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7))"

4 个答案:

答案 0 :(得分:2)

您可以使用以下

获得所需的格式并缩短代码
Range("A4").Resize(12).FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"", TEXT(  INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-(12-rows(R4:R)) ,""00""))"

答案 1 :(得分:0)

你可以格式化整数,总是有2位数。

Format(iMyInt, "00")

答案 2 :(得分:0)

如果您愿意将公式带入VBA,可以使用DatePart更有效地提取周数,然后Format以获得您想要的前导零。

公式的最后一行如下:

Range("A16").FormulaR1C1 = Year(Now()) & "-" & Format(DatePart("ww", Now()), "00")

而第一个是

Range("A4").FormulaR1C1 = Year(Now()) & "-" & Format(DatePart("ww", Now()) - 12, "00")

调用DatePart("ww",Now())告诉VBA返回今天日期的一周部分,将其包装在Format中告诉它以您寻找的格式输出它。

警告,但是 - 这似乎在今天的日期返回值16,因此您可能需要检查您的周定义与DatePart中使用的定义相比,如果您选择使用它。

答案 3 :(得分:0)

当你在VBA中添加公式时,你可能最好在那里得到字段值:

range("A4").value = format(now,"yyyy") & "-" & format(format(dateadd("ww",-x,now),"ww"),"00")

其中x是您想要的周数。

但是,如果您打算使用公式以使其更新而不受任何干扰,那么您可以使用TEXT()函数,就像VBA使用Format()一样。

 Range("A4").FormulaR1C1 = "=CONCATENATE(YEAR(TODAY()),""-"",TEXT((INT((TODAY()-DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3)+WEEKDAY(DATE(YEAR(TODAY()-WEEKDAY(TODAY()-1)+4),1,3))+5)/7)-12),"00"))"

谨慎一点。 VLookup处理基础字段值,因此如果您从显示为2016-15的日期查找,则查找仍将失败。在这种情况下,您可能需要确保将两个日期都设置为工作周的第一天以确保匹配。