Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 61
Default open text file, get file from directory, update file

I have a text file. Lots of rows. The second field is a date yymmdd. It looks
like:

ABC, 091012, 1.47, 1.51, 1.46, 1.495, 586907
XYZ, 091012, ....
etc...

I want to take the value in the first field, in this case "ABC", open a file
called ABC_XXX.csv. I don't care what XXX is, as long as the ABC part matches.

Format of the csv file is:
ABC, 10-Aug-99, 0.9734, 0.9567, etc.
ABC, 11-Aug-99, etc..
etc...

I want to append the row from the text file to the .csv file.

So that the ABC_XXX.csv file has the row:

ABC, 12-Oct-09, 1.47, 1.51, 1.46, 1.495, 586907

added. Date reformatted from 091012 to dd-mmm-yy.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default open text file, get file from directory, update file

Hi Diana

This should do what you want to do. It assumes you are wanting to use
the first field in the current file as the criteria for the CSV file
to open.

Take care

Marcus

Option Explicit
Sub OpenCSVFile()

Dim sPath As String
Dim sFil As String
Dim strName As String
Dim myRow As Integer
Dim PName As String

Rows(1).Copy
PName = Left(Cells(1, 1).Value, 3) ' Column 1 for the csv criteria
sPath = "C:\" ' Change to suit
sFil = Dir(sPath & PName & "*.csv")

Do While sFil < ""

strName = sPath & sFil
Workbooks.Open (strName)
sFil = Dir
Loop
'Pastes to the last row in the CSV file
myRow = Range("A" & Rows.Count).End(xlUp).Row + 1
Cells(myRow, 1).PasteSpecial xlValues
Cells(myRow, 2).NumberFormat = "dd-mmm-yy"

End Sub
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default open text file, get file from directory, update file

If the .csv files are present then you can ignore the second loop...

Sub Macro()

Dim strTextFile As String, strCSVFile As String
Dim intTextFile As Integer, intCSVFile As Integer
Dim strData As String, arrData As Variant
Dim strCSVPath As String, intFile As Integer

strTextFile = "d:\donna.txt"
strCSVPath = "d:"

If Right(strCSVPath, 1) < "\" Then strCSVPath = strCSVPath & "\"
intTextFile = FreeFile
Open strTextFile For Input As #intTextFile
Do While Not EOF(intTextFile)
Line Input #intTextFile, strData
arrData = Split(strData, ",")
arrData(1) = Right(Trim(arrData(1)), 2) & "-" & Format("13-" & _
Mid(Trim(arrData(1)), 3, 2) & "-2009", "MMM") & "-20" &
Left(Trim(arrData(1)), 2)
strCSVFile = Dir(strCSVPath & arrData(0) & "*.csv", vbNormal)
intCSVFile = FreeFile
Open strCSVPath & strCSVFile For Append As #intCSVFile
Print #intCSVFile, Join(arrData, ",")
Close #intCSVFile
Loop
Close #intTextFile

End Sub

--
Jacob


"Jacob Skaria" wrote:

Diana

Try the below

Sub Macro()

Dim strTextFile As String, strCSVFile As String
Dim intTextFile As Integer, intCSVFile As Integer
Dim strData As String, arrData As Variant
Dim strCSVPath As String, intFile As Integer

strTextFile = "d:\donna.txt"
strCSVPath = "d:"

If Right(strCSVPath, 1) < "\" Then strCSVPath = strCSVPath & "\"
intTextFile = FreeFile
Open strTextFile For Input As #intTextFile
Do While Not EOF(intTextFile)
Line Input #intTextFile, strData
arrData = Split(strData, ",")
arrData(1) = Right(Trim(arrData(1)), 2) & "-" & Format("13-" & _
Mid(Trim(arrData(1)), 3, 2) & "-2009", "MMM") & "-20" &
Left(Trim(arrData(1)), 2)
strCSVFile = Dir(strCSVPath & "*.csv", vbNormal)
Do While strCSVFile < ""
If UCase(strCSVFile) Like UCase(arrData(0)) & "_*.CSV" Then
intCSVFile = FreeFile
Open strCSVPath & strCSVFile For Append As #intCSVFile
Print #intCSVFile, Join(arrData, ",")
Close #intCSVFile
Exit Do
End If
strCSVFile = Dir
Loop
Loop
Close #intTextFile

End Sub

--
Jacob


"Diana" wrote:

I have a text file. Lots of rows. The second field is a date yymmdd. It looks
like:

ABC, 091012, 1.47, 1.51, 1.46, 1.495, 586907
XYZ, 091012, ....
etc...

I want to take the value in the first field, in this case "ABC", open a file
called ABC_XXX.csv. I don't care what XXX is, as long as the ABC part matches.

Format of the csv file is:
ABC, 10-Aug-99, 0.9734, 0.9567, etc.
ABC, 11-Aug-99, etc..
etc...

I want to append the row from the text file to the .csv file.

So that the ABC_XXX.csv file has the row:

ABC, 12-Oct-09, 1.47, 1.51, 1.46, 1.495, 586907

added. Date reformatted from 091012 to dd-mmm-yy.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open CSV file, format data and write output to a text file. BristolBloos Excel Programming 1 October 18th 05 03:50 PM
How do I import text file, analyze data, export results, open next file Geoffro Excel Programming 2 March 6th 05 08:02 PM
Combine all text file in directory into one file. Rich[_19_] Excel Programming 8 January 21st 04 04:09 PM
Open delimited text file to excel without changing data in that file zohanc Excel Programming 1 October 3rd 03 01:06 AM
Automate open file, update links, run macro, close and save file Geoff[_7_] Excel Programming 2 August 26th 03 10:13 PM


All times are GMT +1. The time now is 03:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"