Parse CSV, ignoring commas inside string literals in VBA?

后端 未结 11 1898
青春惊慌失措
青春惊慌失措 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:19

    I made another variant of solution for parsing CSV files with "quoted" text strings with possible delimiters, like comma inside the double quotes. This method doesn't require regex expressions, or any other addons. Also, this code deals with multiple commas in between the quotes. Here is Subroutine for testing:

    Sub SubstituteBetweenQuotesSub()
    'In-string character replacement function by Maryan Hutsul      1/29/2019
    Dim quote, quoteTwo As Integer
    Dim oddEven As Integer
    Dim i, counter As Integer
    Dim byteArray() As Byte
    
    'LineItems are lines of text read from CSV file, or any other text string
    LineItems = ",,,2019NoApocalypse.ditamap,jesus.christ@sky.com,Approver,""JC, ,Son"",Reviewer,god.allmighty@sky.com,""God, All-Mighty,"",2019-01-29T08:47:29.290-05:00"
    
    quote = 1
    oddEven = 0
    
    Do Until quote = 0
    quote = InStr(quote, LineItems, Chr(34))
    quoteTwo = InStr(quote + 1, LineItems, Chr(34))
    
    oddEven = oddEven + 1
        If oddEven Mod 2 = 1 And quote <> 0 Then
    
            counter = 0
            For i = quote To quoteTwo
                byteArray = StrConv(LineItems, vbFromUnicode)
                If i <> 0 Then
                    If byteArray(i - 1) = 44 Then   '44 represents comma, can also do Chr(44)
                    counter = counter + 1
                    End If
                End If
            Next i
    
            LineItems = Left(LineItems, quote - 1) & Replace(LineItems, ",", ";", quote, counter)
            quote = quote + 1
        ElseIf quote <> 0 Then
            quote = quote + 1
        End If
    Loop
    
    End Sub
    

    Here is function to which you can pass lines from .csv, .txt or any other text files:

    Function SubstituteBetweenQuotes(LineItems)
    'In-string character replacement function by Maryan Hutsul                                          1/29/2019
    'LineItems are lines of text read from CSV file, or any other text string
    Dim quote, quoteTwo As Integer
    Dim oddEven As Integer
    Dim i, counter As Integer
    Dim byteArray() As Byte
    
    
    quote = 1
    oddEven = 0
    
    Do Until quote = 0
    quote = InStr(quote, LineItems, Chr(34))
    quoteTwo = InStr(quote + 1, LineItems, Chr(34))
    
    oddEven = oddEven + 1
        If oddEven Mod 2 = 1 And quote <> 0 Then
    
            counter = 0
            For i = quote To quoteTwo
                byteArray = StrConv(LineItems, vbFromUnicode)
                If i <> 0 Then
                    If byteArray(i - 1) = 44 Then   '44 represents "," comma, can also do Chr(44)
                    counter = counter + 1
                    End If
                End If
            Next i
    
            LineItems = Left(LineItems, quote - 1) & Replace(LineItems, ",", ";", quote, counter)
            quote = quote + 1
        ElseIf quote <> 0 Then
            quote = quote + 1
        End If
    Loop
    
    SubstituteBetweenQuotes = LineItems
    
    End Function
    

    And below is code for reading CSV file with function used:

    Dim fullFilePath As String
    Dim i As Integer
    
    'fullFilePath - full link to your input CSV file
    Open fullFilePath For Input As #1
    row_number = 0
    column_number = 0
    'EOF - End Of File  (1) - file #1
    Do Until EOF(1)
        Line Input #1, LineFromFile
                LineItems = Split(SubstituteBetweenQuotes(LineFromFile), ",")
        For i = LBound(LineItems) To UBound(LineItems)
        ActiveCell.Offset(row_number, i).Value = LineItems(i)
        Next i
        row_number = row_number + 1
    Loop
    Close #1
    

    All delimiters and replacement character may be modified for your needs. I Hope this is useful as I had quite a journey to solve some problems with CSV imports

提交回复
热议问题