XERO的Quote和Invoice导入DEC (dhlexpresscommerce) | 改进 新增参数自适应 代码自动生成

需求:

在 Excel 中插入了一行参数设定


之前按绝对位置读取的语句就不对了,生成的内容也会是错乱的


所以要在VBA编程中使用VLOOKUP函数

参考:

WorksheetFunction.VLookup(Object, Object, Object, Object) Method
https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.office.interop.excel.worksheetfunction.vlookup

使用 A1 表示法引用单元格和区域
https://docs.microsoft.com/zh-cn/office/vba/excel/concepts/cells-and-ranges/refer-to-cells-and-ranges-by-using-a1-notation


实施:

修改前

input_Code = Worksheets(2).Range("B16").Value

修改后

input_Code = WorksheetFunction.VLookup("Code", Worksheets(2).Range("A:B"), 2, False)


进一步的,我想到可以自动生成这一大段的代码

当然生成的代码只能写在excel表格里,需要手动复制再粘贴到代码编辑器里。

参考

WorksheetFunction.Substitute(String, String, String, Object) Method
https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.worksheetfunction.substitute

字符串表达式中的引号
https://docs.microsoft.com/zh-cn/office/vba/access/concepts/criteria-expressions/include-quotation-marks-in-string-expressions

实施:

Sub buttun2_Click()
    strQuote = Chr(34)

    load_param_code = ""
    i = 1
    Do While i < 30
        param_name = Worksheets(2).Cells(i, 1).Value
        If param_name <> "" Then
            param_str = WorksheetFunction.Substitute(param_name, " ", "_")
            tmp_param_code = "input_" & param_str & " = WorksheetFunction.VLookup(" & strQuote & param_name & strQuote & ", Worksheets(2).Range(" & strQuote & "A:B" & strQuote & "), 2, False)"
            load_param_code = load_param_code & tmp_param_code & Chr(10)
        End If
                       
        i = i + 1
    Loop
    
    Worksheets(2).Cells(i, 1).Value = load_param_code
End Sub

效果:




Github: 

https://github.com/crazypeace/XERO-Quote-or-Invoice-to-DEC-csv

评论

The Hot3 in Last 30 Days

无服务器 自建短链服务 Url-Shorten-Worker 完整的部署教程

ClouDNS .asia免费域名 托管到CloudFlare开CDN白嫖Websocket WS通道翻墙 / desec.io