Using CSV Files

  • Thread starter Thread starter Craig
  • Start date Start date
C

Craig

In a previous post I asked if I could link to a CSV file and it's been
pointed out that this is not possible.

My problem is that my spreadsheet application needs the latest data from a
..CSV file which is downloaded from the web on a regular basis by another
(non Excel) application and is always stored in the same location.

I can write a macro in my application which will load the .CSV file and
then save it as .XLS and then close it. [That way I'm able to link to the
non-open XLS source]

Problem is the macro has to overwrite the previously saved XLS and this
requires manual intervention whereas I need this app to be fully automatic.

Any ideas plse.

TIA
Craig
 
Hello,

If I understand it correctly, I think you need to add the following lines in
your code

Application.DisplayAlerts = False
....Your code to save file
Application.DisplayAlerts = True

Hope this helps!

Jon-jon
 
You got the answer to the question asked but you do not normally need to
save the csv file to xls to get the data. You can open it, use text to
columns if needed, filter or whatever is needed to get your data and then
close it without ever saving as anything.
 
Craig,

You can query the text file (at least with XL2002 -- I don't have 2000, so
not sure). Then you just refresh the query any time you want the latest
data from the text file.

Data - import External Data - Import Data
In File Type box, change to *.txt
Navigate to your file.
The text import wizard will start.
Select fixed or delimited, etc. Continue through the wizard.

A macro can refresh the data when the workbook is opened, or whenever you
want.
 
Craig,

I forgot to mention. To refresh the data, just right click anywhere on it
and choose Refresh.
--
Earl Kiosterud
mvpearl omitthisword at verizon period net
-------------------------------------------

Earl Kiosterud said:
Craig,

You can query the text file (at least with XL2002 -- I don't have 2000, so
not sure). Then you just refresh the query any time you want the latest
data from the text file.

Data - import External Data - Import Data
In File Type box, change to *.txt
Navigate to your file.
The text import wizard will start.
Select fixed or delimited, etc. Continue through the wizard.

A macro can refresh the data when the workbook is opened, or whenever you
want.
--
Earl Kiosterud
mvpearl omitthisword at verizon period net
-------------------------------------------

Craig said:
In a previous post I asked if I could link to a CSV file and it's been
pointed out that this is not possible.

My problem is that my spreadsheet application needs the latest data
from
a
.CSV file which is downloaded from the web on a regular basis by another
(non Excel) application and is always stored in the same location.

I can write a macro in my application which will load the .CSV file and
then save it as .XLS and then close it. [That way I'm able to link to the
non-open XLS source]

Problem is the macro has to overwrite the previously saved XLS and this
requires manual intervention whereas I need this app to be fully automatic.

Any ideas plse.

TIA
Craig
 
I'm not sure what you mean by "link to a CSV file" but you can use VBA code (I'm using Excel 2002) to Input from a file, for example:
Open "C:\temp\thefile.csv" For Input As #1
....
Close #1
This way you can read the data into the workbook -- dump it into a spreadsheet which you can delete later if you need to -- without having to go thru your
current process of importing and Save As...

I'm a VB beginner so I can't help that much with this but there are others who are very talented and they can help here.
Toby Erkson
Oregon, USA
 
You could write a procedure that would open the text file, find the record you
want, parse it out, and return the string. But that sounds like an accident
just waiting to happen.

But if you're living dangerously, maybe this can get you started:

Option Explicit
Function getTextStr(myFilename As String, recNum As Long, _
fieldNumber As Long) As Variant

Dim testStr As String
Dim recCtr As Long
Dim myFileNum As Long
Dim myLine As String
Dim errRec As Boolean
Dim myArr As Variant

testStr = ""
On Error Resume Next
testStr = Dir(myFilename)
On Error GoTo 0

If testStr = "" Then
getTextStr = "File Not Found"
Exit Function
End If

myFileNum = FreeFile()
Close #myFileNum
Open myFilename For Input As #myFileNum
recCtr = 0
errRec = True
Do While Not EOF(myFileNum)
recCtr = recCtr + 1
Line Input #myFileNum, myLine

If recCtr = recNum Then
errRec = False
Exit Do
End If
Loop
Close #myFileNum

If errRec Then
getTextStr = "Record Not Found"
Else
myArr = Split97(myLine, ",")
If UBound(myArr) - LBound(myArr) + 1 < fieldNumber Then
getTextStr = "Field not found"
Else
getTextStr = myArr(fieldNumber - 1)
End If
End If

End Function

Public Function ReadUntil(ByRef sIn As String, _
sDelim As String, Optional bCompare As Long _
= vbBinaryCompare) As String
Dim nPos As String
nPos = InStr(1, sIn, sDelim, bCompare)
If nPos > 0 Then
ReadUntil = Left(sIn, nPos - 1)
sIn = Mid(sIn, nPos + Len(sDelim))
End If
End Function

Public Function Split97(ByVal sIn As String, Optional sDelim As _
String, Optional nLimit As Long = -1, Optional bCompare As _
Long = vbBinaryCompare) As Variant
Dim sRead As String, sOut() As String, nC As Integer
If sDelim = "" Then
Split97 = sIn
End If
sRead = ReadUntil(sIn, sDelim, bCompare)
Do
ReDim Preserve sOut(nC)
sOut(nC) = sRead
nC = nC + 1
If nLimit <> -1 And nC >= nLimit Then Exit Do
sRead = ReadUntil(sIn, sDelim)
Loop While sRead <> ""
ReDim Preserve sOut(nC)
sOut(nC) = sIn
Split97 = sOut
End Function


And you could use it in a worksheet cell like:
=gettextstr("C:\my documents\excel\myfile.csv",13,8)

Where 13 was the record number and 8 is the field to bring back.

The bad news is you may have to be a little careful. If your fields contain
commas:

"my,field,with,commas",1324,test1,test2

It'll screw up the split procedure. But you could parse that yourself, too.
Keep track if you're in a quoted string or out and count the commas or ignore
them.

The last two procedures are stolen from an MSKB.
http://support.microsoft.com/default.aspx?scid=kb;en-us;188007
HOWTO: Simulate Visual Basic 6.0 String Functions in VB5

If you're using xl2k or higher, you can dump these two routines and change
split97 to split. (Split was added in xl2k.)

In a previous post I asked if I could link to a CSV file and it's been
pointed out that this is not possible.

My problem is that my spreadsheet application needs the latest data from a
.CSV file which is downloaded from the web on a regular basis by another
(non Excel) application and is always stored in the same location.

I can write a macro in my application which will load the .CSV file and
then save it as .XLS and then close it. [That way I'm able to link to the
non-open XLS source]

Problem is the macro has to overwrite the previously saved XLS and this
requires manual intervention whereas I need this app to be fully automatic.

Any ideas plse.

TIA
Craig
 
Back
Top