简体   繁体   English

Excel VBA函数做循环和测试标准

[英]Excel VBA Function to Do Loop And Test Criteria

Background information : I am trying to test the future dates to check if they fall on Monday, Wednesday or Saturday. 背景资料 :我正在尝试测试未来的日期,以检查它们是否属于周一,周三或周六。 If the dates falls on one of the three criteria, the date is returned into the cell. 如果日期属于三个标准之一,则日期将返回到单元格中。 I managed to get this part down. 我设法让这部分失效了。

Problem : I would like the function to automatically loop for another 45 days instead of only referencing from the first date. 问题 :我希望该函数能够自动循环另外45天而不是仅从第一个日期开始引用。

Questions : 问题

  • Is there a more efficient way or neater way to write the code? 是否有更有效的方式或更简洁的方式来编写代码?
  • Is it possible to perform loop in functions? 是否可以在函数中执行循环? Is yes, could you show and explain 是的,你能展示和解释吗?
  • What are other possible methods? 还有什么其他可能的方法? I am looking at expanding my knowledge, please share :) 我正在寻求扩展我的知识,请分享:)

Thank you for your time! 感谢您的时间!

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

I would nest the function in a subprocedure. 我会将函数嵌套在子过程中。 The below loops through 45 days (NextOffDay included in those 45) and writes each date that falls on a Mon, Wed or Fri to the next open cell in row 1 of the first worksheet in the workbook the code is written in. 下面循环执行45天(NextOffDay包含在那些45天中)并将每个落在Mon,Wed或Fri上的日期写入编写代码的工作簿中第一个工作表的第1行中的下一个打开单元格。

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

Sub Func_Loop()
Dim NextOffDay as Date
Dim i as Integer

Call Different_Sub 'You simply write Call and the name of the sub procedure.
                   'If it is in a different module, you must qualify which module
                   'If there is an argument it would be: Call Different_Sub(argument)

NextOffDay = "02/25/2015" 'Change this date to whatever you need, or have a user enter the date through an input box or set it to a certain cell.

For i = 0 to 44
If AFPSECONDOFFDAY(NextOffDay + i) <> "" then
    Thisworkbook.Worksheets(1).Cells(1,columns.count).end(xltoleft).offset(0,1) = AFPSECONDOFFDAY(NextOffDay + i)
End if
Next i
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM