Open Files with partial name which is derived from a cell reference and plus undefined part

abhay_547

Board Regular
Joined
Sep 12, 2009
Messages
179
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:
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.
 
Try
Code:
   Dim Workbookpath As String
   Dim Filenameknownpart As String
   Dim Fname As String
   
   With ThisWorkbook.Sheets("Parameters")
      Workbookpath = .Range("A2").Value
      Filenameknownpart = .Range("B2").Value
      Fname = Dir(Workbookpath & "\" & Filenameknownpart & "*.xls*")
      Do While Len(Fname) > 0
          .Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Fname
          Fname = Dir
      Loop
   End With
This will put the filename in the first blank row in col A of Parameters
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try
Code:
   Dim Workbookpath As String
   Dim Filenameknownpart As String
   Dim Fname As String
   
   With ThisWorkbook.Sheets("Parameters")
      Workbookpath = .Range("A2").Value
      Filenameknownpart = .Range("B2").Value
      Fname = Dir(Workbookpath & "\" & Filenameknownpart & "*.xls*")
      Do While Len(Fname) > 0
          .Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Fname
          Fname = Dir
      Loop
   End With
This will put the filename in the first blank row in col A of Parameters


ok. so now i get the filename with the path in the column A so i will use the mid formula in column B and get the only filenames then define them as strings by referring to the cells in column B so that I can navigate back and forth between macro workbook and those files while copying and pasting the data from them into macro workbook. but is there a way I can directly get only filenames in the column A excluding the path so that I don't need to add the formula in Column B and get the filenames which is an additional step.
 
Upvote 0
You shouldn't get path names. When I run it i get


Excel 2013 32 bit
AB
2C:\Users\Fluff\Documents\Excel filesrwc
3RWC 2003.xls
4RWC 2011.xls
5RWC 2015.xls
6RWC 2019.xls
7RWC 2019.xlsx
8RWC2003.xls
Parameters
 
Upvote 0
got it. not sure how it was giving the entire path earlier. I retried and its working fine. all sorted. Thanks a lot :)
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,107
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top