K
K
I have macro set on a button. (see below)
Sub OpenFile()
Folder = "C:\My Document\Records"
FileName = Range("U21").Value
If IsNumeric(FileName) And (Range("U21").Value <> "") And _
Len(FileName) = 6 Then
fName = Dir(Folder & "\*" & FileName & "*.xlsx")
If fName = "" Then
MsgBox "Record *" & FileName & "* DONT EXIST" _
& vbNewLine & "OR" & vbNewLine & "ENTERED INCORRECT NUMBER" _
, vbCritical, "INCORRECT"
Else
Do While fName <> ""
Workbooks.Open Filename:=Folder & "\" & fName
fName = Dir()
Loop
End If
Else
MsgBox "VIREMENT *" & FileName & "* DONT EXIST" _
& vbNewLine & "OR" & vbNewLine & "ENTERED INCORRECT NUMBER" _
, vbCritical, "INCORRECT"
End If
End Sub
at the moment above macro look at the file name in Range("U21") and
find that file in Folder "C:\My Document\Records" and then Open it.
The problem is that there are also Subfolders in Folder "Records" and
I want macro to look in all the Subfolders for the file and when file
is found then Open it. I did try changing the code line
Folder = "C:\My Document\Records"
TO
Folder = "C:\My Document\Records\*"
but it didn’t work. Please can any friend can help that what changes
I should do in above macro that it should look for file in all the
Subfolders and then Open it
Sub OpenFile()
Folder = "C:\My Document\Records"
FileName = Range("U21").Value
If IsNumeric(FileName) And (Range("U21").Value <> "") And _
Len(FileName) = 6 Then
fName = Dir(Folder & "\*" & FileName & "*.xlsx")
If fName = "" Then
MsgBox "Record *" & FileName & "* DONT EXIST" _
& vbNewLine & "OR" & vbNewLine & "ENTERED INCORRECT NUMBER" _
, vbCritical, "INCORRECT"
Else
Do While fName <> ""
Workbooks.Open Filename:=Folder & "\" & fName
fName = Dir()
Loop
End If
Else
MsgBox "VIREMENT *" & FileName & "* DONT EXIST" _
& vbNewLine & "OR" & vbNewLine & "ENTERED INCORRECT NUMBER" _
, vbCritical, "INCORRECT"
End If
End Sub
at the moment above macro look at the file name in Range("U21") and
find that file in Folder "C:\My Document\Records" and then Open it.
The problem is that there are also Subfolders in Folder "Records" and
I want macro to look in all the Subfolders for the file and when file
is found then Open it. I did try changing the code line
Folder = "C:\My Document\Records"
TO
Folder = "C:\My Document\Records\*"
but it didn’t work. Please can any friend can help that what changes
I should do in above macro that it should look for file in all the
Subfolders and then Open it