Solved

File Search using File name in Cell

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now