Solved

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

Posted on 2014-03-20
2
210 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
[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 Comments
 
LVL 52

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

623 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