Solved

File Search using File name in Cell

Posted on 2013-11-15
4
181 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
  • 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 29

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 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