Hi,
I need to import a csv file into excel as text. As this is the only way I can retain all the info in all the cells, ie opening it directly in excel results in missing data, dont ask me why.. I download this file daily. I'd like to design a macro that would perform this function for me, ie import the newest file in my downloads folder as a text file. Following that i have another macro I run on the data.
Thanks in advance!
HW
Hi hotwheels
Leith has code which you can modify form xlms to csv
http://www.excelforum.com/excel-prog...-a-folder.html
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Thanks for that link.
What I am looking for is to import the latest file as text, rather than open it directly. I could probably use some of that code to find the most recent file, however i'm no expert in reading VBA code. Your help appreciated!
HW
hi hotwheels
sound like the csv file is a xls file with csv extension
Has the csv file been save correctly?
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Your post does not comply with Rule 7 of our Forum RULES. Please do not ignore Moderators' or Administrators' requests - note that this includes requests by senior members as well, if you are unclear about their request or instruction then send a private message to them asking for help. Do not post a reply to a thread where a moderator has requested an action that has not been complied with e.g Title change or Code tags...etc
please address Roys request in your previous thread
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Thanks for the previous messages on this thread. this thread is now unblocked so any furhter help would be appreciated.
Thanks,
HW
hi hotwheels
your can change the directory and sheet # to suit
Option Explicit Sub ptest() Dim xDir As String, csvFile As String, LastestFile As String Dim ModDate As Date, LastDate As Date Dim FileObj As Object, wsXLS As Object, wbCSV As Object Set wsXLS = ThisWorkbook.Sheets("Sheet1") ' Set FileObj = CreateObject("Scripting.FileSystemObject") xDir = "C:\Documents and Settings\ect....." csvFile = Dir(xDir & "\*.csv") Do While csvFile <> "" ModDate = FileObj.GetFile(xDir & "\" & csvFile).DateLastModified If LastDate < ModDate Then LastestFile = csvFile LastDate = ModDate End If csvFile = Dir() Loop Debug.Print LastestFile & " " & LastDate Set wbCSV = Workbooks.Open(xDir & "\" & LastestFile) Range("A1").CurrentRegion.Copy wsXLS.Cells(1, wsXLS.Columns.Count).End(xlToLeft).Offset(0, 1) wbCSV.Close Set wsXLS = nothing Set FileObj = nothing Set wbCSV = nothing End Sub
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Pike,
Your code does open the latest file in the folder. Due to the characteristics of the file that I am opening however I lose data when I do it this way. In order to not lose data I use the Get External Data>From Text commands.
I probably wasn't clear enough in my first post. Does this make it clearer? Principally I'm looking for a macro to retrieve data from the latest file in a folder using the Get External Data command.
Thanks
You can see from Pike's code how he gets the filename to open. Then, his line of code shows he is opening the found filename with this:
Set wbCSV = Workbooks.Open(xDir & "\" & LastestFile)
So you should be able to take that same reference:
And use that in your GetData code. Give it a try. Merge your method of file extraction with his method of finding the filename.xDir & "\" & LastestFile
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Thanks JBeau for the encouragement.
This is the recording of the steps I use to get the data I want:
I'm a little unsure how to join this code to that supplied by Pike. I'm not particularly good at reading code but I guess the key is in the first couple of lines.Sub getdatalatestfile() ' ' getdatalatestfile Macro ' ' Keyboard Shortcut: Ctrl+Shift+W ' With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\Admin\Downloads\Station vélo_ On street__ Yes Publicly Available__ Yes (27).csv" _ , Destination:=Range("$A$1")) .Name = "Station vélo_ On street__ Yes Publicly Available__ Yes (27)" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 850 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub
I think I'd start the code with the first part of Pikes macro:
But where would I insert:Option Explicit Sub newestfileinfolderastext() Dim xDir As String, csvFile As String, LastestFile As String Dim ModDate As Date, LastDate As Date Dim FileObj As Object, wsXLS As Object, wbCSV As Object Set wsXLS = ActiveWorkbook.Sheets("Sheet1") ' Set FileObj = CreateObject("Scripting.FileSystemObject") xDir = "C:\Users\Admin\Downloads" csvFile = Dir(xDir & "\*.csv") Do While csvFile <> "" ModDate = FileObj.GetFile(xDir & "\" & csvFile).DateLastModified If LastDate < ModDate Then LastestFile = csvFile LastDate = ModDate End If csvFile = Dir() Loop
?xDir & "\" & LastestFile
Yep, I'm still beginning at this.
HW
Like so:
Option Explicit Sub GetDataLatestFile() ' ' getdatalatestfile Macro ' ' Keyboard Shortcut: Ctrl+Shift+W ' Dim xDir As String, csvFile As String, LatestFile As String Dim ModDate As Date, LastDate As Date Dim FileObj As Object, wsXLS As Object, wbCSV As Object Set wsXLS = ActiveWorkbook.Sheets("Sheet1") Set FileObj = CreateObject("Scripting.FileSystemObject") xDir = "C:\Users\Admin\Downloads" xDir = "C:\2010" csvFile = Dir(xDir & "\*.csv") Do While csvFile <> "" ModDate = FileObj.GetFile(xDir & "\" & csvFile).DateLastModified If LastDate < ModDate Then LatestFile = csvFile LastDate = ModDate End If csvFile = Dir() Loop If LatestFile = "" Then MsgBox "No file found" Exit Sub End If With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & xDir & "\" & LatestFile, Destination:=Range("$A$1")) .Name = LatestFile .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 850 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Great, thanks for this. It works a dream. I just updated the date to 2011.
Will get this thread marked as solved.
Actually the line of code I accidentally left in:
....can be removed. That was for testing on my system.xDir = "C:\2010
Last edited by JBeaucaire; 03-06-2011 at 08:18 PM.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks