Solved

File Search using File name in Cell

Posted on 2013-11-15
4
182 Views
Last Modified: 2013-11-19
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!!
0
Comment
Question by:leezac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39652580
Here is a macro that will take a path from cell B11 on the active worksheet, then look for a file with "Revised.xls" as the last part of the filename. If none is found, it will look for any .xls file in that folder. If either file is found, it will be opened.
Sub GetFile()
Dim flName As String, flPath As String
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 Workbooks.Open (flPath & flName)
End Sub

Open in new window

0
 
LVL 30

Expert Comment

by:gowflow
ID: 39653565
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
0
 

Author Comment

by:leezac
ID: 39656517
thank you - looking at now
0
 

Author Closing Comment

by:leezac
ID: 39659689
Thanks for help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question