+ Reply to Thread
Results 1 to 6 of 6

Code update - the latest found file or specific file

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2015
    Location
    CZECH
    MS-Off Ver
    Office 365
    Posts
    53

    Code update - the latest found file or specific file

    Hello experts,

    I gues for you easy topic - could you please help me to update below written code:

    Variant 1:
    specific csv file name open with StrPath - for example open "data.csv" in C:\Users\Documents\ (not GetOpenbyFileName), the rest code same

    Variant 2:
    add to code method to find "the freshest" csv file in specific folder, (for example C:\Users\Documents\) and give data from file, the rest code same

    THANK YOU VERY MUCH. Have a nice day.
    vendam

    Sub test()
        Dim fn As String, txt As String, x, y, n As Long
        Dim myCols, colRef, i As Long, ii As Long
        fn = Application.GetOpenFilename("CSVFies,*.csv")
        If fn = "False" Then Exit Sub
        myCols = Array("FEATURE CODE", "Point_ID", "Easting", "Northing", "ELEVATION", "Remark1")
        x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbCrLf)
        colRef = Filter(WorksheetFunction.IfError(Application.Match(myCols, Split(x(0), ","), 0), False), False, 0)
        ReDim a(1 To UBound(x) + 1, 1 To UBound(colRef) + 1)
        For i = 0 To UBound(x)
            If x(i) <> "" Then
                y = Split(x(i), ","): n = n + 1
                For ii = 0 To UBound(colRef)
                    a(i + 1, ii + 1) = y(colRef(ii) - 1)
                Next
            End If
        Next
        Range("a2").Resize(n, UBound(a, 2)).Value = a
    End Sub

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,279

    Re: Code update - the latest found file or specific file

    Sub test_Variant1()
        Dim fn As String, txt As String, x, y, n As Long
        Dim myCols, colRef, i As Long, ii As Long
        Dim strPath As String
        
        strPath = Environ$("UserProfile") & Application.PathSeparator
        fn = strPath & "data.csv"
    '    fn = Application.GetOpenFilename("CSVFies,*.csv")
    '    If fn = "False" Then Exit Sub
        myCols = Array("FEATURE CODE", "Point_ID", "Easting", "Northing", "ELEVATION", "Remark1")
        x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbCrLf)
        colRef = Filter(WorksheetFunction.IfError(Application.Match(myCols, Split(x(0), ","), 0), False), False, 0)
        ReDim a(1 To UBound(x) + 1, 1 To UBound(colRef) + 1)
        For i = 0 To UBound(x)
            If x(i) <> "" Then
                y = Split(x(i), ","): n = n + 1
                For ii = 0 To UBound(colRef)
                    a(i + 1, ii + 1) = y(colRef(ii) - 1)
                Next
            End If
        Next
        Range("a2").Resize(n, UBound(a, 2)).Value = a
    End Sub

  3. #3
    Registered User
    Join Date
    07-10-2015
    Location
    CZECH
    MS-Off Ver
    Office 365
    Posts
    53

    Re: Code update - the latest found file or specific file

    Dear dangelor,
    perfect, thank you very much for help. Works as requested.
    Variant 1: solved
    vendam

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,279

    Re: Code update - the latest found file or specific file

    Sub test_Variant2()
        Dim fn As String, txt As String, x, y, n As Long
        Dim myCols, colRef, i As Long, ii As Long
        Dim strPath As String, strFile As String
        Dim d As Date, dLatest As Date
        
        strPath = Environ$("UserProfile") & Application.PathSeparator
        strFile = Dir(strPath & "*.csv", vbNormal)
        
        Do While Len(strFile) > 0
            d = FileDateTime(strPath & strFile)
            If d > dLatest Then
                fn = strFile
                dLatest = d
            End If
            strFile = Dir
        Loop
        
    '    fn = Application.GetOpenFilename("CSVFies,*.csv")
    '    If fn = "False" Then Exit Sub
        myCols = Array("FEATURE CODE", "Point_ID", "Easting", "Northing", "ELEVATION", "Remark1")
        x = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll, vbCrLf)
        colRef = Filter(WorksheetFunction.IfError(Application.Match(myCols, Split(x(0), ","), 0), False), False, 0)
        ReDim a(1 To UBound(x) + 1, 1 To UBound(colRef) + 1)
        For i = 0 To UBound(x)
            If x(i) <> "" Then
                y = Split(x(i), ","): n = n + 1
                For ii = 0 To UBound(colRef)
                    a(i + 1, ii + 1) = y(colRef(ii) - 1)
                Next
            End If
        Next
        Range("a2").Resize(n, UBound(a, 2)).Value = a
    End Sub

  5. #5
    Registered User
    Join Date
    07-10-2015
    Location
    CZECH
    MS-Off Ver
    Office 365
    Posts
    53

    Re: Code update - the latest found file or specific file

    Dear dangelor,
    perfect! Variant 2: solved as well.
    thank you very much for help.

    Have a nice weekend.

    vendam

  6. #6
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,279

    Re: Code update - the latest found file or specific file

    You are welcome!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to update hyperlink with latest saved name of linked file
    By phillb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2018, 02:07 AM
  2. [SOLVED] Vba code to compile specific details from different excel file to one file from folder
    By UPA in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-25-2017, 07:38 AM
  3. [SOLVED] VBA code to open latest file
    By mashiulalam in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-15-2016, 10:08 AM
  4. [SOLVED] If file isnt found, navigate to another specific file.
    By CAD Blaster in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-10-2013, 04:57 AM
  5. Replies: 9
    Last Post: 11-28-2013, 05:20 PM
  6. [SOLVED] Code for opening file if match with file name is found
    By cokillerliu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-25-2013, 08:41 PM
  7. Excel 2010 file with VBA save and send code works-but file can't be found.
    By carlton.clay in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2013, 09:46 AM

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