[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


VBA Search for Formula Text in Cell

Posted on 2014-08-26
Medium Priority
Last Modified: 2014-08-26

I found the following VBA code that works perfectly when searching for a text string across multiple workbooks in a desired folder.  I am trying to accomplish a similar task but instead search for linked data sources by searching for the brackets contained in the cell formula.

For example, I want the VBA to loop through multiple Excel workbooks looking for formulas that contain the left and right bracket; thus letting me know the workbook contains a linked data source.

The code below works like a charm when looking for specific text versus text in a formula cell.

Example Excel Workbook with formula in cell B2

123      =VLOOKUP(A1,'C:\Users\johndoe\Documents\Test\[book2.xlsx]Sheet1'!$A$1:$B$3,2,FALSE)
456      B  
789      C

VBA Code
Sub SearchWkBooks()

Dim WS As Worksheet
Dim myfolder As String
Dim Str As String
Dim a As Single
Dim sht As Worksheet
Set WS = Sheets.Add
With Application.FileDialog(msoFileDialogFolderPicker)
    myfolder = .SelectedItems(1) & "\"
End With
Str = Application.InputBox(prompt:="Search string:", Title:="Search all workbooks in a folder", Type:=2)
If Str = "" Then Exit Sub
WS.Range("A1") = "Search string:"
WS.Range("B1") = Str
WS.Range("A2") = "Path:"
WS.Range("B2") = myfolder
WS.Range("A3") = "Workbook"
WS.Range("B3") = "Worksheet"
WS.Range("C3") = "Cell Address"
WS.Range("D3") = "Link"
a = 0
Value = Dir(myfolder)
Do Until Value = ""
    If Value = "." Or Value = ".." Then
        If Right(Value, 3) = "xls" Or Right(Value, 4) = "xlsx" Or Right(Value, 4) = "xlsm" Then
            On Error Resume Next
            Workbooks.Open Filename:=myfolder & Value, Password:="zzzzzzzzzzzz"
            If Err.Number > 0 Then
                WS.Range("A4").Offset(a, 0).Value = Value
                WS.Range("B4").Offset(a, 0).Value = "Password protected"
                a = a + 1
                On Error GoTo 0
                For Each sht In ActiveWorkbook.Worksheets
                        Set c = sht.Cells.Find(Str, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
                        If Not c Is Nothing Then
                            firstAddress = c.Address
                                WS.Range("A4").Offset(a, 0).Value = Value
                                WS.Range("B4").Offset(a, 0).Value = sht.Name
                                WS.Range("C4").Offset(a, 0).Value = c.Address
                                WS.Hyperlinks.Add Anchor:=WS.Range("D4").Offset(a, 0), Address:=myfolder & Value, SubAddress:= _
                                sht.Name & "!" & c.Address, TextToDisplay:="Link"
                                a = a + 1
                                Set c = sht.Cells.FindNext(c)
                            Loop While Not c Is Nothing And c.Address <> firstAddress
                        End If
                Next sht
            End If
            Workbooks(Value).Close False
            On Error GoTo 0
        End If
    End If
    Value = Dir
End Sub

Open in new window

Question by:KANEDA 0149
LVL 24

Accepted Solution

Ejgil Hedegaard earned 2000 total points
ID: 40286511
The similar search for a text as part of a formula is
str = "["
Set c = sht.Cells.Find(Str, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

If you just want to know if there are links in the workbook, you don't have to loop all sheets and cells, searching for [, use LinkSources.
It is an array with the link specifications, and if it is empty, there are no links in the file.

If Not IsEmpty(ActiveWorkbook.LinkSources) Then
    'The code to display that the file has links.
End If

Author Closing Comment

by:KANEDA 0149
ID: 40286621
Nice, thank you Ejgil Hedegaard.  It worked exactly as I needed!

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

872 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