I am making a little report system that needs to get the name of a file from an excel cell and look for that in 12 possible folders. all the folders are labeled "TXT-XXX" where XXX is a specific month. So I would like to have it dynamic. I tried using wildcards to represent "XXX" but it keeps giving me a bad file name error. Currently if there is no value, it opens the explorer to look for a file. that part works and I can import the txt files just fine. but the dynamic part does not work.
the possible XXX file names are: "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
Here is the part of the code that I am refering to.
Dim my_file As Integer
Dim text_line As String
Dim File_Name As String
Dim i As Integer
Dim worknumber As String
worknumber = Range("K5").Value
If worknumber = "" Then
File_Name = Application.GetOpenFilename
Else
File_Name = "U:TXT-***" & worknumber & ".txt"
End If
my_file = FreeFile()
Open File_Name For Input As my_file
i = 1
While Not EOF(my_file)
Line Input #my_file, text_line
Cells(i, "A").Value = text_line
i = i + 1
Wend
question from:
https://stackoverflow.com/questions/66050735/get-vba-code-in-excel-to-find-a-file-and-open-it 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…