VBA Reading txt file vs Excel file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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"