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
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?
Re: Open Files with just partname
Quote:
Originally Posted by
jdc2000
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.
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
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