1

so i searched but didnt find something good for my use , i have a folder where i will import an excel file , this excel file will have a different name everytime how i can open it with vba , thank you

ana bel
  • 177
  • 1
  • 15
  • 1
    Will there be only one file in the directory or there can me more than one? – Siddharth Rout Apr 04 '19 at 13:22
  • What have you tried to build, or what is the closest thing you've foudn so far? In addition to that, what specifically does not work with those/that example? Similar to Siddharth's question, please provide more detail about the current situation and the desired situation. – Cyril Apr 04 '19 at 13:25
  • [This](https://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba/10382861#10382861) will get you started ;) – Siddharth Rout Apr 04 '19 at 13:26
  • You could also look at document properties using VBA. – JvdV Apr 04 '19 at 15:21

1 Answers1

1

You can get the file name using the Dir function and multiple character (*) wildcard.

Const Path As String = "C:\Test"

Dim filename As String
    filename = Dir(Path & "\*.xlsx")

If Len(filename) > 0 Then
    ' Do your work 
    ' Remember 'filename' only holds the file name
    ' you will need to attach the rest of the path to get the full directory.
End If 

Note: If there's only one file in the folder you will not have any issues, however if the folder contains multiple files (matching the above pattern), you will need to either loop or provide additional file name characters to the function.

An example:

File name: daily_report_20190404.xlsx

filename = Dir(Path & "\daily_report_*.xlsx")

Hope this helps.

Kostas K.
  • 8,293
  • 2
  • 22
  • 28