背景信息: 我想测试未来日期以检查它们是否在周一,周三或周六。如果日期符合三个标准之一,则日期将返回到单元格中。我设法解决了这个问题。Excel VBA函数做循环和测试标准
问题:我希望函数自动循环45天,而不是仅从第一个日期引用。
问题:
- 有没有更有效的方法或更合适的方法来写代码?
- 是否有可能在函数中执行循环?是的,你能展示并解释
- 什么是其他可能的方法?我正在扩大我的知识,请分享:)
谢谢你的时间!
Public Function AFPSECONDOFFDAY(NextOffDay As Date) As String
Dim ans As String, dayCount As Integer, n As Integer
n = 2
' This statement returns the date into the cell if it is Monday, Wednesday or Saturday
If Format(NextOffDay + 2, "w", vbMonday) = 1 _
Or Format(NextOffDay + 2, "w", vbMonday) = 3 _
Or Format(NextOffDay + 2, "w", vbMonday) = 6 Then
' would like it to continue for another 45 days instead
' of stopping after + 2 days
AFPSECONDOFFDAY = NextOffDay + 2
Else
'If the date does not meet the criteria, I would like it to _
skip and do the next date. So that cells will be filled simultaneously.
AFPSECONDOFFDAY = ""
End If
End Function
获得这个循环是很容易的部分。您希望如何返回这些信息?你是否想要将所有日期返回到输入的单元格中? – Kyle 2015-02-23 16:34:53
请继续向右偏移至最后。第一个单元格,第二个单元格和之后:) – Kelvinlimjk 2015-02-23 16:46:28