如何在VBA中的Workday函数的Holiday_array中传递Access Column值?

时间:2016-09-23 13:40:13

标签: vba excel-vba ms-access access-vba ms-access-2013

如何在假日范围列表中的MS Excel 2010 Workday函数中传递MS Access表列值。

我有下表。

date_val
9/23/2016
9/24/2016

我想在假期列表中的MS Excel 2010 Workday函数中使用上面的日期列表。

示例工作日(今天,添加没有天,排除假期)

按照上述Workday函数的语法,我正在寻找 工作日(任何日期,添加一天,排除上面列出的MS访问表列日期)。

任何人都可以在工作日函数中帮助如何在holiday_array中传递MS Access Table列日期值吗?

注意:代码需要在MS Access VBA环境中实现。

1 个答案:

答案 0 :(得分:2)

只要 将数组 传递给Excel函数,就可以从Access VBA调用它。

General usage of function described here

子程序有两个例子

Public Sub TestWorkdayFunction()

    Dim xlApp       As Excel.Application

    Dim startDate   As Date
    Dim numDays     As Long
    Dim arrDates    As Variant
    Dim nextDate    As Date

    Dim strDates    As String            

    ' Array of Holidays
    arrDates = Array("1/1/2016", "3/25/2016", "3/28/2016")

    Set xlApp = CreateObject("Excel.Application")

Debug.Print "Loading Array of Holidays From Preset List"

    startDate = #12/30/2015#
Debug.Print startDate

    numDays = 1
    nextDate = xlApp.WorksheetFunction.WorkDay(startDate, numDays, arrDates)
    Debug.Print "Next Work Day after " & numDays & ": " & Format(nextDate, "Long Date")

    numDays = 2
    nextDate = xlApp.WorksheetFunction.WorkDay(startDate, numDays, arrDates)
    Debug.Print "Next Work Day after " & numDays & ": " & Format(nextDate, "Long Date")

Debug.Print "Loading Array of Holidays From Recordset"

    Dim rs  As DAO.Recordset

    Set rs = CurrentDb.OpenRecordset("SELECT date_val FROM Table2", dbOpenSnapshot, dbReadOnly)
    With rs
        While Not .EOF
            ' Build comma separated list of dates (in Serial Date format)
            ' You could build date list with text format, enclosing in double quotes
            strDates = strDates & DateSerial(Year(!date_val), Month(!date_val), Day(!date_val)) & ","
            .MoveNext
        Wend
        .Close
    End With

    startDate = #3/24/2016#
Debug.Print startDate

    ' Remove Last comma
    strDates = Left$(strDates, Len(strDates) - 1)

    ' Build Array of Holiday Dates
    arrDates = Split(strDates, ",")

    numDays = 1
    nextDate = xlApp.WorksheetFunction.WorkDay(startDate, numDays, arrDates)
    Debug.Print "Next Work Day after " & numDays & ": " & Format(nextDate, "Long Date")

    numDays = 5
    nextDate = xlApp.WorksheetFunction.WorkDay(startDate, numDays, arrDates)
    Debug.Print "Next Work Day after " & numDays & ": " & Format(nextDate, "Long Date")

    Set rs = Nothing
    Set xlApp = Nothing

End Sub

实际调试输出

Loading Array of Holidays From Preset List
12/30/2015 
Next Work Day after 1: Thursday, December 31, 2015
Next Work Day after 2: Monday, January 04, 2016
Loading Array of Holidays From Recordset
3/24/2016 
Next Work Day after 1: Tuesday, March 29, 2016
Next Work Day after 5: Monday, April 04, 2016