Solved

Want source file name to be listed in results of Excel macro

Posted on 2014-03-20
2
199 Views
Last Modified: 2014-03-20
I have just used the code provided in ID24183402.  It worked very well and I would like to be able to add the source file name to be listed in the results.  Thanks!

Sub FindAll()
   Dim wks As Worksheet, wksOut As Worksheet
   Dim colFoundRanges As Collection
   Dim strFind As String
   Dim lngIndex As Long
   On Error Resume Next
   Application.DisplayAlerts = False
   Sheets("Find results").Delete
   Application.DisplayAlerts = True
   Set wksOut = Worksheets.Add
   wksOut.Name = "Find results"
   
   Set colFoundRanges = New Collection
   
   strFind = InputBox("Enter string to find")
   For Each wks In ActiveWorkbook.Worksheets
      FindCells wks, strFind, colFoundRanges
   Next wks
   With colFoundRanges
      For lngIndex = 1 To .Count
         With .Item(lngIndex)
            wksOut.Cells(lngIndex, 1).Value = .Parent.Name
            wksOut.Cells(lngIndex, 2).Value = .Address
            wksOut.Cells(lngIndex, 3).Value = "'" & .Formula
         End With
      Next lngIndex
   End With
End Sub
Sub FindCells(ws As Worksheet, strToFind As String, colOutput As Collection)
   Dim strFirstAddress As String
   Dim rngFound As Range
   With ws.UsedRange
      Set rngFound = .Find(what:=strToFind, lookat:=xlPart, LookIn:=xlFormulas, MatchCase:=False)
      If Not rngFound Is Nothing Then
         strFirstAddress = rngFound.Address
         Do
            colOutput.Add rngFound, ws.Name & "-" & rngFound.Address
            Set rngFound = .FindNext(rngFound)
         Loop While rngFound.Address <> strFirstAddress
      End If
   End With
End Sub 

Open in new window

0
Comment
Question by:LMPhillips
2 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39943148
Hi,

pls try (.Parent.Parent.Name)

Sub FindAll()
   Dim wks As Worksheet, wksOut As Worksheet
   Dim colFoundRanges As Collection
   Dim strFind As String
   Dim lngIndex As Long
   On Error Resume Next
   Application.DisplayAlerts = False
   Sheets("Find results").Delete
   Application.DisplayAlerts = True
   Set wksOut = Worksheets.Add
   wksOut.Name = "Find results"
   
   Set colFoundRanges = New Collection
   
   strFind = InputBox("Enter string to find")
   For Each wks In ActiveWorkbook.Worksheets
      FindCells wks, strFind, colFoundRanges
   Next wks
   With colFoundRanges
      For lngIndex = 1 To .Count
         With .Item(lngIndex)
            wksOut.Cells(lngIndex, 1).Value = .Parent.Parent.Name
            wksOut.Cells(lngIndex, 2).Value = .Parent.Name
            wksOut.Cells(lngIndex, 3).Value = .Address
            wksOut.Cells(lngIndex, 4).Value = "'" & .Formula
         End With
      Next lngIndex
   End With
End Sub
Sub FindCells(ws As Worksheet, strToFind As String, colOutput As Collection)
   Dim strFirstAddress As String
   Dim rngFound As Range
   With ws.UsedRange
      Set rngFound = .Find(what:=strToFind, lookat:=xlPart, LookIn:=xlFormulas, MatchCase:=False)
      If Not rngFound Is Nothing Then
         strFirstAddress = rngFound.Address(, , , True)
         Do
            colOutput.Add rngFound, ws.Name & "-" & rngFound.Address
            Set rngFound = .FindNext(rngFound)
         Loop While rngFound.Address <> strFirstAddress
      End If
   End With
End Sub 

Open in new window

Regards
0
 
LVL 1

Author Closing Comment

by:LMPhillips
ID: 39943167
Just what I needed and so quickly!  Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

744 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

10 Experts available now in Live!

Get 1:1 Help Now