VBA Reading txt file vs Excel file

VBA Reading txt file vs Excel file

pball
Mentor Mentor
1,296 Views
1 Reply
Message 1 of 2

VBA Reading txt file vs Excel file

pball
Mentor
Mentor

I was reading a list of vendors out of a text file in one of my iproperties scripts, then I switched to reading the list from an excel file since that is the original master list. I noticed a lot of lag in my script and tracked it down to the code reading the excel file. I'd expect reading an Excel file would take longer than a plain text file but it's taking 1.5 seconds to read from an Excel file vs 0.1 second for a text file. The location of the files local or network do not seem to have any noticible effects on read time.

 

This is the first time I've had a script access an Excel file so I don't know if this is normal. Perhaps there is better or different code. Anyone have experience with this?

 

Code to read a txt file with a vendor name per line

    startTime = Timer
    fpath = full_path(ThisApplication.FileOptions.DefaultVBAProjectFileFullFilename) & "required\vendors.txt"

    If Len(Dir$(fpath)) <> 0 Then
        'Requires "Microsoft Scripting Runtime" to be enabled (scrrun.dll)
        With New Scripting.FileSystemObject
            With .OpenTextFile(fpath, ForReading)
                If Not .AtEndOfStream Then .SkipLine
                Do Until .AtEndOfStream
                    AddItem (.ReadLine), Me.Vendor_List
                Loop
            End With
        End With
    End If
    MsgBox Format(Timer - startTime, "0.00") & " seconds"

Code to read an Excel file with a vender name per cell starting at cell A2 on sheet named LISTS

    startTime = Timer
    fpath = "L:\000 Encompix\EBOM Import Template REV 2.2.2.xlsx"

    Dim objWorkbook As Workbook
    Set objWorkbook = Workbooks.Open(fpath)

    num = 2
    While (objWorkbook.Worksheets("LISTS").Cells(num, 1) <> "")
        AddItem objWorkbook.Worksheets("LISTS").Cells(num, 1), Me.Vendor_List
        num = num + 1
    Wend
    
    objWorkbook.Close (False)
    MsgBox Format(Timer - startTime, "0.00") & " seconds"

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
1,297 Views
1 Reply
Reply (1)
Message 2 of 2

rossano_praderi
Collaborator
Collaborator

Hi,

is absolutely normal, while you are reading a text file your script simply reads every line as is.
While you read an excel file the informations are more complex than a text file.

 

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------