+ Reply to Thread
Results 1 to 13 of 13

importing the newest file in a folder as text

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    importing the newest file in a folder as text

    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

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: importing the newest file in a folder as text

    Hi hotwheels

    Leith has code which you can modify form xlms to csv

    http://www.excelforum.com/excel-prog...-a-folder.html
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: importing the newest file in a folder as text

    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

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: importing the newest file in a folder as text

    hi hotwheels
    sound like the csv file is a xls file with csv extension
    Has the csv file been save correctly?

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: importing the newest file in a folder as text

    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

  6. #6
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: importing the newest file in a folder as text

    Thanks for the previous messages on this thread. this thread is now unblocked so any furhter help would be appreciated.

    Thanks,

    HW

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: importing the newest file in a folder as text

    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

  8. #8
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: importing the newest file in a folder as text

    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

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: importing the newest file in a folder as text

    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:
    xDir & "\" & LastestFile
    And use that in your GetData code. Give it a try. Merge your method of file extraction with his method of finding the filename.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon 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!)

  10. #10
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: importing the newest file in a folder as text

    Thanks JBeau for the encouragement.

    This is the recording of the steps I use to get the data I want:

    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'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.

    I think I'd start the code with the first part of Pikes macro:
    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
    But where would I insert:

    xDir & "\" & LastestFile
    ?

    Yep, I'm still beginning at this.

    HW

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: importing the newest file in a folder as text

    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

  12. #12
    Registered User
    Join Date
    10-24-2010
    Location
    Melbourne, Aust
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: importing the newest file in a folder as text

    Great, thanks for this. It works a dream. I just updated the date to 2011.

    Will get this thread marked as solved.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: importing the newest file in a folder as text

    Actually the line of code I accidentally left in:
    xDir = "C:\2010
    ....can be removed. That was for testing on my system.
    Last edited by JBeaucaire; 03-06-2011 at 09:18 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1