0

我一直在努力解决这个问题几个小时,并且一直碰壁。我非常接近,但有一些小细节错误。目前我在excel vba中使用=weeknum来动态创建一行并接收#NUM!错误。当我引用一个单元格时它似乎有效,但是当我手动输入一个日期时,我得到类型不匹配错误并改变其他错误。我使用的日期格式是 6/3/2013

非常感谢您的任何帮助,并感谢您的宝贵时间!

Sub Macro2()
    Dim xDate As Double
    Dim ReturnType As Integer

    TotalRowCount = ThisWorkbook.Sheets("sap").UsedRange.Rows.count
    Set entryDateRange = ThisWorkbook.Sheets("sap").Range("G2:G" & TotalRowCount)
'my attempt at setting the datatype for the row, I dont think this is needed, but was one solution I saw
    'Range("M2:M" & TotalRowCount).NumberFormat = "@"
'create a collumn to hold the fiscal week output
    ThisWorkbook.Sheets("sap").Range("M1") = "FW"

    For Each test In entryDateRange
        CurrentRow = test.Row
        fw_calc = Application.Evaluate("=WEEKNUM(G" & CurrentRow & "," & ReturnType & ")")
        ' In place of (G" & CurrentRow.. etc I would prefer to use test if possible
        Worksheets("sap").Cells(CurrentRow, 13) = fw_calc
    Next test
End Sub
4

2 回答 2

1

好的,我看到了你的问题。尝试使用以下行:

fw_calc = Application.Evaluate("=WEEKNUM(DATE(" & Year(test.value) & "," & Month(test.value) & ", " & Day(test.value) & "))")

为了更清楚地解释,该WEEKNUM()函数需要一个excel序列日期,所以我们需要先创建一个。执行此操作的功能是DATE(),它需要单独的年、月和日(按此顺序)。所以我们使用 VBA 函数Day()Month()Year()从日期单元格 () 中解析出三个组件test,然后将它们传递给DATE()函数,如下所示:

"DATE(" & Year(test.value) & ", " & Month(test.value) & ", " & Day(test.value) & ")"

这将返回日期序列。然后我们将该序列作为参数传递WEEKNUM以获取周数。看起来有点尴尬,但你明白了。

于 2013-07-17T14:45:06.250 回答
0
fw_calc = Application.WeekNum(test, ReturnType)

但是您没有给出ReturnType值,因此它的默认值为 0,这是不可接受的值。如果省略,则默认为 1(星期日)一周开始的那一天。

于 2013-07-17T15:08:41.933 回答