Sub 作業日を作成()
    Dim mCode As Variant
    Dim formulaText As String
    Dim csvPath As String
    Dim ws As Worksheet
    Dim lo As ListObject

    csvPath = ThisWorkbook.Path & "\7-9-注文データ.csv"

    mCode = Array( _
        "let", _
        "    ソース = Csv.Document(File.Contents(""" & Replace(csvPath, "\", "\\") & """), [Delimiter="","", Encoding=65001, QuoteStyle=QuoteStyle.Csv]),", _
        "    ヘッダーの昇格 = Table.PromoteHeaders(ソース, [PromoteAllScalars=true]),", _
        "    型変更 = Table.TransformColumnTypes(ヘッダーの昇格,{{""注文日"", type date}, {""顧客名"", type text}, {""商品名"", type text}, {""納期"", type date}}),", _
        "    作業可能日数の追加 = Table.AddColumn(型変更, ""作業可能日数"", each Duration.Days([納期] - [注文日]), Int64.Type)", _
        "in", _
        "    作業可能日数の追加" _
    )

    formulaText = Join(mCode, vbCrLf)

    ThisWorkbook.Queries.Add _
        Name:="作業日", _
        Formula:=formulaText

    Set ws = ThisWorkbook.Worksheets("Sheet1")

    Set lo = ws.ListObjects.Add( _
        SourceType:=0, _
        Source:=Array( _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;" & _
            "Data Source=$Workbook$;" & _
            "Location=作業日;" & _
            "Extended Properties=""""" _
        ), _
        Destination:=ws.Range("A1") _
    )

    lo.Name = "T_作業日"

    With lo.QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [作業日]")
        .Refresh BackgroundQuery:=False
    End With

    ThisWorkbook.Queries("作業日").Delete
End Sub