View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
marcus[_3_] marcus[_3_] is offline
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