Results 1 to 12 of 12

Thread: [SOLVED] Parsing CSV is randomly slow

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Resolved [SOLVED] Parsing CSV is randomly slow



    i've write a code that import a csv file in an excel worksheet

    the code works fine but, with the same csv file, the speed of the tests are sometimes very quick and other times very slow

    here is my code :


    Code:
    Sub Import_CSV()
        Dim pcti As Single, pctj As Single, dept As Single
        
        pcti = Timer
        pctj = Timer
        dept = Timer
        
        'here i select my csv file
        FileToOpen = Application.GetOpenFilename("Fichier CSV (*.csv), *.csv", , "Sélectionner le fichier à importer ...", , False)
        If FileToOpen = False Then
            Exit Sub
        End If
        
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set Fichier = objFSO.OpenTextFile(FileToOpen, 1, True)
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(FileToOpen, 1, False)
        
        Application.ScreenUpdating = False
        
        i = 1
        Do Until Fichier.AtEndOfStream
            j = i - 1
            tab_line = Split(Fichier.ReadLine, ";")
        
            If i >= 2 Then
                Feuil1.Range("A" & j).Value = tab_line(0)
                Feuil1.Range("B" & j).Value = tab_line(1)
                Feuil1.Range("C" & j).Value = tab_line(2)
                Feuil1.Range("D" & j).Value = tab_line(3)
                Feuil1.Range("E" & j).Value = tab_line(4)
                Feuil1.Range("F" & j).Value = tab_line(5)
                Feuil1.Range("G" & j).Value = tab_line(6)
            End If
    
            i = i + 1
        Loop
    
        'here i clear the unwated cells
        j = i - 1
        Feuil1.Range("A" & G, "J65000").ClearContents
        
        Application.ScreenUpdating = True
        Unload u_progression
    End Sub
    my csv file is semicolon formated
    and contains about 8000 lines
    exemple :
    Code:
    Nbre;dtehre;Heures;Debits Sens 1;Données Reconstituées; Données (année - 1);ID
    1;2009-03-05 00:00:00;00;0;49;49;38611
    2;2009-03-05 01:00:00;01;0;20;20;38612
    3;2009-03-05 02:00:00;02;0;28;28;38613
    4;2009-03-05 03:00:00;03;0;15;15;38614
    5;2009-03-05 04:00:00;04;0;7;7;38615
    6;2009-03-05 05:00:00;05;0;7;7;38616
    .....
    .....
    with the same file, sometimes it take 1 minute max, and sometimes about 20min
    and it seems its very randomly (i tried to quit and execute excel, boot and reboot computer, etc ...) but never be able to find a cause

    so maybe its something in the code, maybe is there something i ccan inprove

    thanks to help
    bye
    Last edited by schlagg; Mar 15th, 2010 at 03:31 AM. Reason: solved

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Parsing CSV is randomly slow

    Welcome to the forums

    Would something like this speed up your process?

    Code:
    FileToOpen = "C:\Mycsv.Csv"
    
    Workbooks.OpenText Filename:= _
    FileToOpen, Origin:=437, _
    StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False _
    , Space:=False, TrailingMinusNumbers:=True
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Parsing CSV is randomly slow

    Hi,

    thanks for the welcome and the answer

    so, i allready test this way, yes its very fast (almost no delay) but the problem is that it opens a new worksheet and me i have to stock the data in an existing worksheet (cause i've another worksheet which build a chart with those data)

    if there is a way to import like in your code AND put the data in an existing worksheet, i think my problem will disapear

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Parsing CSV is randomly slow

    Show me your existing workbook...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Parsing CSV is randomly slow

    if you import the data to a new sheet /book, you can then copy it to a position on an existing sheet, then remove the imported sheet / book
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Parsing CSV is randomly slow

    here are the files in attachement.

    westconn1 > what is the code for that? thanks
    Attached Files Attached Files

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Parsing CSV is randomly slow

    As I knew, at least to Excel 2003:
    If use the Workbooks.OpenText method to open a file with extension ".csv",
    Excel will open the file as a native Excel file with comma (,) delimited and
    it will ignore all setting on other delimiters.

    Particular in this case, two parameters Semicolon:=True, Comma:=False will have no effect.
    Excel will treat the file with Comma:=True (and all other delimiters :=False)

    * I don't know the reason why the code uses these 4 lines together:
    Code:
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set Fichier = objFSO.OpenTextFile(FileToOpen, 1, True)
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(FileToOpen, 1, False)
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Parsing CSV is randomly slow

    i deleted those 2 lines
    Code:
    Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(FileToOpen, 1, False)
    they indeed are not necessary but script continue to work like before

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Parsing CSV is randomly slow

    no one has any clue???

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Parsing CSV is randomly slow

    as anhn says you can not correctly import a .csv file that is not csv (comma separated values), but easy enough to rename to .txt, then import
    but as you filling is conditional, it may not help a lot

    your existing code will be quicker if you populate a 2 dimensional array first then copy the array to the worksheet when finished

    vb Code:
    1. dim arr2() as variant
    2. redim arr2 (1 to n, 1 to 10)
    3.     i = 1
    4.     Do Until Fichier.AtEndOfStream
    5.         j = i - 1
    6.         tab_line = Split(Fichier.ReadLine, ";")
    7.    
    8.         If i >= 2 Then
    9.             arr2(j, 1) = tab_line(0)
    10.             arr2(j, 2) = tab_line(1)
    11.             arr2(j, 3) = tab_line(2)
    12.             arr2(j, 7) = tab_line(6)
    13.                        
    14.             If i >= 3 Then
    15.                 arr2(j, 9) = ""     ' i presume you can remove any lines that assign empty values
    16.                 arr2(j, 10) = ""
    17.                 If tab_line(3) = 0 Then
    18.                     arr2(j, 4) = ""
    19.                     arr2(j, 5) = tab_line(4)
    20.                     arr2(j, 6) = ""
    21.                 Else
    22.                     arr2(j, 4) = tab_line(3)
    23.                     arr2(j, 5) = ""
    24.                     arr2(j, 6) = tab_line(5)
    25.                 End If
    26.             Else
    27.                arr2(j, 4) = tab_line(3)
    28.                arr2(j, 5) = tab_line(4)
    29.                arr2(j, 6) = tab_line(5)
    30.             End If
    31.         End If

    at the end of the loop when finished reading csv file
    fill the worksheet like
    vb Code:
    1. Feuil1.Range("a1").resize(n, 10) = arr2
    this is untested, so i may have made some typo
    test and see if any improvement
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Parsing CSV is randomly slow

    This will take you 1 second to import the file.
    Code:
    Sub Import_CSV()
        Dim FileToOpen As Variant
        Dim fs As Object
        Dim ts As Object
        Dim sLines() As String
        Dim tab_entete() As String
        Dim arr() As Variant
        Dim n As Long
        Dim r As Long
        Dim i As Long
        
        FileToOpen = Application.GetOpenFilename("Fichier CSV (*.csv), *.csv", , "S&#233;lectionner le fichier &#224; importer ...", , False)
        If FileToOpen = False Then Exit Sub
        
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set ts = fs.OpenTextFile(FileToOpen, 1, False)
        sLines = Split(ts.ReadAll, vbLf)
        ts.Close
        Set ts = Nothing
        Set fs = Nothing
        
        Application.ScreenUpdating = False
        
        tab_entete = Split(sLines(0), ";")
        n = Val(tab_entete(0))
        With Feuil1
            .UsedRange.ClearContents
            .Range("H1") = tab_entete(1)
            .Range("H2") = tab_entete(2)
            .Range("H3") = tab_entete(3)
        End With
        
        r = UBound(sLines)
        Do While Trim(sLines(r)) = "":  r = r - 1:  Loop
        
        ReDim arr(1 To r, 1 To 1)
        For i = 1 To r:   arr(i, 1) = sLines(i):   Next
        
        With Feuil1
            .Range("A1:A" & r) = arr
            .Range("A1:A" & r).TextToColumns Destination:=.Range("A1"), _
                    DataType:=xlDelimited, Semicolon:=True, Tab:=False, Comma:=False, _
                    Space:=False, Other:=False, ConsecutiveDelimiter:=False, FieldInfo:= _
                    Array(Array(1, 1), Array(2, 5), Array(3, 1), Array(4, 1), _
                          Array(5, 1), Array(6, 1), Array(7, 1))
            arr = .Range("D2:F" & r)
            For i = 1 To r - 1
                If arr(i, 1) = 0 Then
                    arr(i, 1) = ""
                    arr(i, 3) = ""
                Else
                    arr(i, 2) = ""
                End If
            Next
            .Range("D2:F" & r) = arr
        End With
        Application.ScreenUpdating = True
        
        Sheet1.ScrollBar1.Max = n - 23
        Unload u_progression
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    6

    Re: Parsing CSV is randomly slow

    anhn and westconn1 > thanks for reply and tips

    i don't tests them 'cause i find this post http://www.vbforums.com/showthread.php?t=562369 and it solve my problem

    i test them when i've time 'cause the method is different and maybe i'll have to edit some lines before pasting them in excel.

    thanks again for support

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width