[英]Excel Fiscal Week Calculator
I've been working on solving this for a few hours now and keep hitting a wall. 我已经在解决这个问题上工作了几个小时,并且一直碰壁。 I am very close but having small detail errors. 我非常接近,但有一些细节错误。 Currently I am using =weeknum in excel vba to dynamically create a row and receiving a #NUM! 目前,我在Excel vba中使用= weeknum动态创建一行并收到#NUM! error. 错误。 It seems to work when I reference a cell but when I manual enter a date I get type mismatch errors and varies other errors. 当我引用一个单元格时,它似乎起作用,但是当我手动输入日期时,出现类型不匹配错误并改变了其他错误。 The date format i am using is 6/3/2013 我使用的日期格式是6/3/2013
Any help is greatly appreciated and thank you for your time! 非常感谢您的帮助,感谢您的宝贵时间!
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
OK I see your problem. 好的,我知道您的问题。 Try using the following line: 尝试使用以下行:
fw_calc = Application.Evaluate("=WEEKNUM(DATE(" & Year(test.value) & "," & Month(test.value) & ", " & Day(test.value) & "))")
To explain more clearly, the WEEKNUM()
function wants an excel serial date, so we need to create one first. 为了更清楚地说明, WEEKNUM()
函数需要一个excel序列号,因此我们需要先创建一个。 The function to do that with is DATE()
, which requires a separate year, month and day (in that order). 要执行此操作的函数是DATE()
,它需要一个单独的年,月和日(按此顺序)。 So we use the VBA functions Day()
, Month()
and Year()
to parse out the three components from your date cell ( test
), then pass them in to the DATE()
function like so: 因此,我们使用VBA函数Day()
, Month()
和Year()
来解析日期单元格( test
)中的三个组成部分,然后将它们传递给DATE()
函数,如下所示:
"DATE(" & Year(test.value) & ", " & Month(test.value) & ", " & Day(test.value) & ")"
This returns the date serial. 这将返回日期序列。 Then we pass that serial as the argument to WEEKNUM
to get the week number. 然后,将该序列作为参数传递给WEEKNUM
以获取星期数。 Looks a bit awkward, but you get the idea. 看起来有点尴尬,但您明白了。
fw_calc = Application.WeekNum(test, ReturnType)
but you haven't given ReturnType
a value so it has its default value of 0, which is not an acceptable value. 但是您没有给ReturnType
赋值,因此它的默认值为0,这是一个不可接受的值。 If omitted it defaults to 1 (Sunday) the day the week begins. 如果省略,则默认为星期几开始的1(星期日)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.