The code for the function (to place in a module in excel):
Code:
Private Function ReadTickerCSV(MyFileName As String) As String ' Dim all vars Dim MyFileNumber As Integer, I As Integer Dim MyFileIsOpen As Boolean Dim ErrMsg As String ' Allocate a free filenumber MyFileNumber = FreeFile ' Trap errors if any occur On Error GoTo ErrHandler ' Clear Previous Errors if any Err.Clear ' Create fake error (for debugging) - Remmed-out in normal version ' Err.Raise 6 ' Open the file Open MyFileName For Input Access Read Shared As #MyFileNumber ' If we got here the file has opened successfully MyFileIsOpen = True ' Read the entire contents in one single step ReadTickerCSV = Trim(Str(LOF(MyFileNumber))) & Chr(0) & _ Input(LOF(MyFileNumber), MyFileNumber) ErrHandler: ' Close the file if still open If MyFileIsOpen Then Close #MyFileNumber ' If/when error trapped If Err Then ReadTickerCSV = "Error #" & Str(Err.Number) End If ' Reset error trapping On Error GoTo 0 End Function
Code:
Sub TestReading() ' Initiate timer sTimer = Timer ' Dim all vars Dim MyPath As String, MyFileName As String, DataFromFile As String Dim BytesRead As Long, CellsRead As Long ' Set FileName to your file MyPath = ActiveWorkbook.Path MyFileName = MyPath & "\TickData.csv" ' Read the data DataFromFile = ReadTickerCSV(MyFileName) ' If/when no error occured If Not Mid(DataFromFile, 1, 7) = "Error #" Then BytesRead = Val(Mid(DataFromFile, 1, InStr(1, DataFromFile, Chr(0)))) DataFromFile = Mid(DataFromFile, InStr(1, DataFromFile, Chr(0)) + 1) BytesRead2 = Len(DataFromFile) CellsRead = 501 * 50 ' Display info from 2 rows for test - modify hardware to your own values MsgTxt = "Test Hard- & Software:" & vbNewLine & _ "Computer: Acer NetbookOne" & vbNewLine & _ "Processor: Intel Atom N270 Dual Core @ 1.60 GHz" & vbNewLine & _ "Memory: 1024 MB @ 800 MHz + Windows managed page-file" & vbNewLine & _ "Operating System: Windows XP SP3 installed" & vbNewLine & _ "Office version: Excel2003 - VBA version: 6.5.1024" & vbNewLine & _ vbNewLine & "Test Results:" & vbNewLine & _ "Bytes read from file = " & Format(BytesRead, "###,###,###") & vbNewLine & _ "Cells read from file = " & Format(CellsRead, "###,###,###") & vbNewLine & _ vbNewLine & _ "File Contents (Limited to 2 records): " & vbNewLine & _ Mid(DataFromFile, 1, 538) & vbNewLine & _ "Total time wasted in function " & Format(Timer - sTimer, "#0.####0") & " seconds." MsgTitle = " Test-result For Reading a .CSV-file From Within Ms-Excel2003" MsgBox MsgTxt, vbInformation, MsgTitle Else ' Display the error if/when one occured MsgBox DataFromFile & " caused this function to end unexpectedly." & _ vbNewLine & "Please contact the author for debugging." End If End Sub
Speed is not an issue, the function reads 170.050 bytes (corresponding with the values of 25.050 cells in the .Csv-file) in 0.03125 seconds !!
Since i will only need to read about 2.500 cells from the .csv-file, the transfer can be done without the user noticed "something" happend.
Left to do is:
1) The excel-part
a) Write a function to "cut" the data-string in 2.500 "cells" so that they can be copied to their corresponding cells in the worksheet.
b) Write a function to write a .csv-file from the ticker-list in excel (max 50 tickers or 50 cells)
2) The Pb-Part
a) Read needed tickers from the .csv-file
b) Download the .csv-file from Yahoo
c) Store the downloaded file in the current dir
With a little luck, this weekend i can post the complete code here ...

Leave a comment: