+ Reply to Thread
Results 1 to 16 of 16

Macro to open a file when only a partial name is known

  1. #1
    Registered User
    Join Date
    08-13-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    19

    Macro to open a file when only a partial name is known

    I'm having a problem, I need to open a file e.g "C:\1523002327_ADP_Tab_Din_II(R).xls" in a macro however I only know the last characters "ADP_Tab_Din_II(R).xls" at the start is a random 10 digit reference number which I have no way of predicting , is there any way I can open this file knowing only C:\ADP_Tab_Din_II(R).xls?

    Any help would be very much appreciated!

  2. #2
    Registered User
    Join Date
    07-15-2015
    Location
    South East England
    MS-Off Ver
    2010 & 2013
    Posts
    72

    Re: Macro to open a file when only a partial name is known

    Is the file you are trying to open the newest file in the folder?
    Happy to help, if I did please Add Reputation

  3. #3
    Registered User
    Join Date
    08-13-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to open a file when only a partial name is known

    yes, but i have other files in the same folder.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to open a file when only a partial name is known

    Is that the only file with that ending?

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to open a file when only a partial name is known

    Try:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-13-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to open a file when only a partial name is known

    No...after 3 hours i received a new file with different name eg.

    First File name received at 03:00 am: 1523002327_ADP_Tab_Din_II(R).xls
    Second File Name received at 06:00 am: 1545003548_ADP_Tab_Din_II(R).xls
    Third File Name receveid at 09:00 am: 1546983254_ADP_Tab_Din_II(R).xls

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to open a file when only a partial name is known

    ?? is there any way I can open this file knowing only C:\ADP_Tab_Din_II(R).xls? Did you want to open all those files?

  8. #8
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Macro to open a file when only a partial name is known

    @John H. Davis, I may be wrong but I think you need to include folder path in the workbooks.open event (unless you call it from a workbook in the same directory).

    Alternative,
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-15-2015
    Location
    South East England
    MS-Off Ver
    2010 & 2013
    Posts
    72

    Re: Macro to open a file when only a partial name is known

    I think the file in question is the newest in the Directory

  10. #10
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Macro to open a file when only a partial name is known

    Use this function to get the full name of the latest file matching a partial pattern:
    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Macro to open a file when only a partial name is known

    For the newest file in the directory, try:
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    08-13-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to open a file when only a partial name is known

    Quote Originally Posted by JOHN H. DAVIS View Post
    ?? is there any way I can open this file knowing only C:\ADP_Tab_Din_II(R).xls? Did you want to open all those files?
    Hi John....i have all files in the same Directory, and i need open the most recent file with getting the partial name.

    Is not can be only the most recent file because i have another files with different names and more recent than this one.
    Last edited by hbender; 08-18-2015 at 09:59 AM.

  13. #13
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Macro to open a file when only a partial name is known

    i have all files in the same Directory, and i need open the most recent file with getting the partial name.
    Is not can be only the most recent file because i have another files with different names and more recent than this one.
    Hi hbender
    Does my post in #10 not give you exactly this?

  14. #14
    Registered User
    Join Date
    08-13-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to open a file when only a partial name is known

    Hi guys.

    @cyiangou

    Here is my problem....i'm not good with VB and i need a help to change the VB code =(

    The code is responsible for open another file in the corporate network (with or without password), copy a range selection, close the file without save it and paste the selection in this spreadsheet. This code worked very well
    My new problem is i have more files into the same shared folder C:\Importa. and i need open the most recent file with getting the partial name.
    Is not can be only the most recent file because i have another files with different names and more recent than this one.


    This is my vb code and feel free to make any change.


    Sub Copiar_Dados()
    Dim wsOrigem As Worksheet
    Dim wsDestino As Worksheet
    Dim wbk As Workbook
    Dim passwd As String

    passwd = "teste"

    'File Destinations, open first
    'Set the path of the same

    caminhocompleto = "C:\Importa\1523002327_ADP_Tab_Din_II(R).xls"

    'as the worksheet is protected by password necessary to open the file before you select.
    'Opening the file as read / write, open as read-only gives error.
    Set wbk = Workbooks.Open(Filename:=caminhocompleto, ReadOnly:=False, Password:=passwd)
    'hides the file so that the user does not see the same.
    wbk.Windows(1).Visible = False



    'Files and Tabs Origin and Destination
    Set wsOrigem = Workbooks("1523002327_ADP_Tab_Din_II(R).xls").Worksheets("ADP")
    Set wsDestino = Workbooks("ADP_Oficial.xlsm").Worksheets("ADP")

    With wsOrigem
    wsDestino.Range("B5:CJ30000").ClearContents
    .Range("a2:CI30000").Copy Destination:=wsDestino.Range("B5:CJ30000")

    End With

    'Close the Destination File and Save
    Workbooks("ADP.xlsx").Close SaveChanges:=False


    End Sub

  15. #15
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Macro to open a file when only a partial name is known

    Hi hbender

    Copy the function from post #10 below your sub in the module, then change this line in your code:
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.
    and this line:
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.
    I haven't checked whether the rest of your code does what you need it to do. I've only given you the part where you resolve the fully qualified pathname of the latest file.

    Edit: I hope someone else can help you with the rest.

  16. #16
    Registered User
    Join Date
    08-13-2012
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Macro to open a file when only a partial name is known

    Thanks a lot cyiangou

    The vb code work very well.

+ 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 file with partial name
    By sabubakaralis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2014, 09:42 AM
  2. Need Macro file to split then consolidate workbooks based on partial file name
    By dawnmau in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2014, 04:37 PM
  3. Find, open and work with file based on partial name
    By nteil in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-31-2013, 09:37 AM
  4. Save As macro with only partial file path
    By jake.newport in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-24-2013, 03:42 PM
  5. [SOLVED] Assigning Variable Based on Partial Name of an Open File
    By lsteinbach in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2013, 05:28 PM
  6. Lookup & open file based on partial filename
    By Im2bz2p345 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2011, 07:04 PM
  7. Open a file do a macro ( made) and open next succesive file
    By SVTman74 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-21-2006, 05: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