Link to home
Start Free TrialLog in
Avatar of leezac
leezac

asked on

File Search using File name in Cell

I have a file path located in say B11 and want to be able to search for the file.  

1.  Search for file using path in B11
The problem is there may be two files and I want the one that has
 c:\test\filename.xls  or c:\test\filename_revised.xls

2.  If the file found has  "revised" I want to open that file

3.  Then go to Sheet2 row 4 and copy paste into current workbook on Sheet2 row 4

I have not worked that much with directory searches and need help.

Thanks in advance!!
ASKER CERTIFIED SOLUTION
Avatar of byundt
byundt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not for points. Here is the complete answer to your question. Byundt only provided part1.

Pls check the attached file and just put a value in cell B11 and activate the button on sheet1 check results in sheet2.

Here is the code

Sub GetFileandCopytoSheet2()
Dim flName As String, flPath As String
Dim WB As Workbook
Dim WS As Worksheet
Dim WS2 As Worksheet

Set WS = ActiveSheet
Set WS2 = Sheets("Sheet2")


flPath = ActiveSheet.Range("B11").Value
If Right(flPath, 1) <> Application.PathSeparator Then flPath = flPath & Application.PathSeparator
flName = Dir(flPath & "*Revised.xls")
If flName = "" Then flName = Dir(flPath & "*.xls")
If flName <> "" Then
    Set WB = Workbooks.Open(flPath & flName)
    WB.Sheets(1).UsedRange.Copy
    WS2.Range("A4").PasteSpecial
    MsgBox ("File " & WB.Name & " Copied successfully.")
    WB.Close False
Else
    MsgBox ("No file found to be copied.")
End If

End Sub

Open in new window



gowflow
CopyRev.xls
Avatar of leezac
leezac

ASKER

thank you - looking at now
Avatar of leezac

ASKER

Thanks for help.