+ Reply to Thread
Results 1 to 3 of 3

Open files that have a variable date format in the name

Hybrid View

MiloDjukanovic Open files that have a... 06-12-2016, 06:57 AM
JBeaucaire Re: Open files that have a... 06-12-2016, 08:52 AM
MiloDjukanovic Re: Open files that have a... 06-12-2016, 01:28 PM
  1. #1
    Registered User
    Join Date
    06-05-2010
    Location
    Belgrade
    MS-Off Ver
    Excel 2007
    Posts
    2

    Open files that have a variable date format in the name

    Macros (attached) task is to open today's file from a folder, copy its contents in Excel sheet and close it.
    The files are saved by the application with a strange criteria. Random saves files in formats "d.m.yyyy" or "d.mm.yyyy" or "dd.m.yyyy" or "dd.mm.yyyy" (like "7.5.2016-K.xml" or "7.05.2016-K.xml" or "07.5.2016-K.xml" or "07.05.2016-K.xml"), and I can not affect it.
    The macro works perfectly until a date is in ONE of the format above.
    Is there a possibility to declare "myToday = Format(Date, "dd.m.yyyy")" with multiple criteria which will include all date formats above, so the macro identifies the file regardless of the date format.

    Thanks

    Sub CpyCntnt()
    Dim strLine As String
    Dim i As Integer
    Dim myToday As String
    Dim strFileName As String
    
    myToday = Format(Date, "dd.m.yyyy")
    strFileName = myToday & "-K" & ".xml"
    Open "C:\Archive\" & strFileName For Input As #1
    i = 1
    While EOF(1) = False
        'read the next line of data in the text file
        Line Input #1, strLine
        'print the data in the current row
        Cells(i, 1) = strLine
        'increment the row counter
        i = i + 1
    Wend
    Close #1
    End Sub
    Last edited by JBeaucaire; 06-12-2016 at 08:46 AM.

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

    Re: Open files that have a variable date format in the name

    Something like this:
    Option Explicit
    
    Sub CpyCntnt()
    Dim strLine As String, strFileName As String, i As Long
    
    strFileName = Dir("C:\Archive\" & Format(Date, "dd.m.yyyy") & "-K" & ".xml")
    If strFileName = "" Then strFileName = Dir("C:\Archive\" & Format(Date, "d.mm.yyyy") & "-K" & ".xml")
    If strFileName = "" Then strFileName = Dir("C:\Archive\" & Format(Date, "dd.mm.yyyy") & "-K" & ".xml")
    If strFileName = "" Then
        MsgBox "Dated file not found in tested formats, new one may be in use."
        Exit Sub
    End If
    
    Open "C:\Archive\" & strFileName For Input As #1
    i = 1
    While EOF(1) = False
        Line Input #1, strLine  'read the next line of data in the text file
        Cells(i, 1) = strLine   'print the data in the current row
        i = i + 1               'increment the row counter
    Wend
    Close #1
    
    End Sub
    _________________
    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!)

  3. #3
    Registered User
    Join Date
    06-05-2010
    Location
    Belgrade
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Open files that have a variable date format in the name

    You are wrong, not "Something like this", just this.

    Thanks so much

+ 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. Open files within date range
    By originalJ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-24-2016, 12:37 PM
  2. macro to open files and copy specific sheet into new workbook (have open files part)
    By dangerdavedsp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-04-2015, 02:23 PM
  3. Replies: 4
    Last Post: 09-01-2014, 07:57 PM
  4. Macro to open multiple files, remove header and save multiple files in a new format
    By twocircles in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-17-2014, 05:24 PM
  5. [SOLVED] Macro to open files with diffrent date format
    By suyogpatil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-28-2013, 07:10 AM
  6. Macro to open files based on string variable
    By Coeus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-11-2013, 06:47 PM
  7. Macro to open, copy/paste, and close files with variable name
    By mjr33 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2011, 10:20 PM

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