Results 1 to 5 of 5

Thread: Open Files with just partname

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Open Files with just partname

    Hi,

    I am trying to open a file for which I have the partial name .i.e. starting part in a excel cell defined as a string and the ending part is some unique code which keeps changing so can't be hard coded, so here is how i am trying to open the same but it's not working. can someone help
    Code:
    'Code:
    Dim Workbookpath As String
    Dim Filenameknownpart As String
    
    Workbookpath = ThisWorkbook.Sheets("Parameters").Range("A2").Value
    Filenameknownpart  = ThisWorkbook.Sheets("Parameters").Range("B2").Value
    
    Workbooks.Open Filename:= Workbookpath & "\" & Filenameknownpart & "******" & ".xls"

    Note: i have used asterisk to define the unique code which is six digit as of now but may go up, that is beyond 6 digit in future so don't want to keep it limited to 6 digits, also it's unique everyday so can't be hard coded anyway.


    Previously posted on mrexcel but not got the solution yet. below is the link:

    https://www.mrexcel.com/forum/excel-...ined-part.html

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Open Files with just partname

    You have to have the full name of the file to open it. If you don't know what the 6 (or more) digits are that may be part of it, you will have to find out what those are before you can open the file. As posted in your other link, you will need to use the Dir function to get the full name of the file. You did not specify if there will only be one Excel file of this type in the folder, or if there can be more than one. If there is more than one file that might in this folder with the naming format you are looking for, how will you determine which one you should open, or do you need to process all of the files of this name format?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Re: Open Files with just partname

    Quote Originally Posted by jdc2000 View Post
    You have to have the full name of the file to open it. If you don't know what the 6 (or more) digits are that may be part of it, you will have to find out what those are before you can open the file. As posted in your other link, you will need to use the Dir function to get the full name of the file. You did not specify if there will only be one Excel file of this type in the folder, or if there can be more than one. If there is more than one file that might in this folder with the naming format you are looking for, how will you determine which one you should open, or do you need to process all of the files of this name format?
    you are right, there could be 2 or more files with the same partname but anyways I want to process all the files with the knownpartname, so it should open all.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Open Files with just partname

    are there many files with the same knownpart in the same folder?

    you can return a file using a partname, but of course you have to supply enough information to identify the file uniquely

    Code:
    filename = dir(workbookpath & "\" & fileknownpart & "*.xls*)
    Workbooks.Open Filename:= Workbookpath & "\" & filename
    but if there are multiple files matching the fileknownpart it will just open the first, you can loop through all the files in the folder that match the fileknownpart, but i am not sure how that can fit to your existing question
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Open Files with just partname

    Try something like this:

    Code:
    Sub ProcessExcelFiles()
    
        Dim strExcelFiles As String
        Dim strFileName As String, strCurrentFile As String, strImportFile As String
        Dim strPathName As String    
        Dim strFileNameKnownPart As String
        
        ' Default names for files
        strFileNameKnownPart = "<YourDataHere>"
        strExcelFiles = strFileNameKnownPart & "*.xls"
        
        strFileName = strPathName & strExcelFiles
        strCurrentFile = Dir(strFileName)
        Do While strCurrentFile <> ""
            strImportFile = strPathName & strCurrentFile
            ' Open and process the file here
            Workbooks.Open Filename:= strImportFile
            
            ' Get next file to process
            strCurrentFile = Dir
        Loop
    
    End Sub

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width