我可以在子中有一个功能吗?

时间:2016-12-05 10:22:51

标签: vba excel-vba excel

Public Function WorkDayDiff(ByRef StartDate As Range) As Integer

        Dim Counter As Integer
        For Counter = 1 To DateDiff("d", StartDate.Value, Now())
            If Weekday(CDate(StartDate.Value + Counter)) > 1 And Weekday(CDate(StartDate.Value + Counter)) < 7 Then
            WorkDayDiff = WorkDayDiff + 1
        Next Counter
End Function

我需要在Sub中使用此函数的值WorkDayDiff。有没有办法可以将这个函数放在使用该值的Sub中?

1 个答案:

答案 0 :(得分:1)

读到这个:

http://www.cpearson.com/excel/differen.htm

与此同时,你可以从一个sub调用它,但不能把它放在sub中,如果我理解你的话你想要这个。

sub MainSub()
    dim myDate as range
    set myDate = sheets("Sheet1").range("A1")
    WorkDayDiff myDate
end sub
Public Function WorkDayDiff(ByRef StartDate As Range) As Integer

        Dim Counter As Integer
        For Counter = 1 To DateDiff("d", StartDate.Value, Now())
            If Weekday(CDate(StartDate.Value + Counter)) > 1 And Weekday(CDate(StartDate.Value + Counter)) < 7 Then
            WorkDayDiff = WorkDayDiff + 1
        Next Counter
End Function