Parse CSV, ignoring commas inside string literals in VBA?

后端 未结 11 1900
青春惊慌失措
青春惊慌失措 2020-12-30 03:27

I have a VBA application that runs every day. It checks a folder where CSVs are downloaded automatically, and adds their contents to a database. When parsing them, I reali

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 04:22

    Try This! Make sure to have the "Microsoft VBScript Regular Expressions 5.5" ticked on References under Tools.

    enter image description here

    Function Splitter(line As String, n As Integer)
    Dim s() As String
    Dim regex As Object
        Set regex = CreateObject("vbscript.regexp")
        regex.IgnoreCase = True
        regex.Global = True
        regex.Pattern = ",(?=([^\""]*\""[^\""]*\"")*[^\""]*$)"
        s = split(regex.Replace(line, "|/||\|"), "|/||\|")
        Splitter = s(n - 1)
    End Function
    

提交回复
热议问题