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-10-売上金額0除算サンプル.csv"

    mCode = Array( _
        "let", _
        "    ソース = Csv.Document(File.Contents(""" & Replace(csvPath, "\", "\\") & """), [Delimiter="","", Encoding=65001, QuoteStyle=QuoteStyle.Csv]),", _
        "    ヘッダーの昇格 = Table.PromoteHeaders(ソース, [PromoteAllScalars=true]),", _
        "    型変更 = Table.TransformColumnTypes(ヘッダーの昇格,{{""売上No"", type text}, {""顧客名"", type text}, {""商品名"", type text}, {""売上金額"", Int64.Type}, {""人数"", Int64.Type}}),", _
        "    人数単価の追加 = Table.AddColumn(型変更, ""人数単価"", each try [売上金額] / [人数] otherwise null)", _
        "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
