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
Solved

File Search using File name in Cell

Posted on 2013-11-15
4
180 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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

809 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