PDA

View Full Version : Enhancement of "Find text in Workbook and create link to cells"



Phlow
06-23-2014, 05:11 AM
Dear all,

I'm fairly new to VBA and have found your website very interesting.
I found a code which works really well and would almost suit my needs: it's called "Find text in Workbook and create link to cells" from "mdmackillop"
The only thing I would need is a little adjustment:
On each sheet I have reference data in Cells D1 and E1. This data I would also like to copy into the target sheet "FindWord".
So, if the code finds target word in a sheet, not only create hyperlink and tell on which sheet the word can be found but also copy the values from Cell D1 and E1.
Some how I don't get the data I need but instead other data.
I believe it has something to do with the "how" I read the range, "copy" it to the array and "paste" it into the "FindWord" sheet but I don't have a clue what I'm doing wrong. :banghead:
Help would be much appreciated.
This is the code I have (I have highlighted my own adjustments in RED):

Option Compare Text
Option Explicit

Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long

Const SM_CXSCREEN = 0

'Returns screen size to set display column width
Private Function ScreenWidth()
ScreenWidth = GetSystemMetrics(SM_CXSCREEN)
End Function

Sub DoFindAll()
'Arguments required for initial use in a workbook
FindAll "", "True"
End Sub


Public Sub FindAll(Search As String, Reset As Boolean)

Dim WB As Workbook
Dim WS As Worksheet
Dim Cell As Range
Dim Prompt As String
Dim Title As String
Dim FindCell() As String
Dim FindSheet() As String
Dim FindWorkBook() As String
Dim FindPath() As String
Dim FindText() As String
Dim Counter As Long
Dim FirstAddress As String
Dim Path As String
Dim MyResponse As VbMsgBoxResult
Dim CopyRng As Range
Dim FindRng() As String

If Search = "" Then
Prompt = "Please enter isin code" & vbNewLine & vbNewLine & Path
Title = "Search Criteria Input"
'Delete default search term if required
Search = InputBox(Prompt, Title, "Enter search term")
If Search = "" Then
GoTo Canceled
End If
End If

Application.DisplayAlerts = False
Application.ScreenUpdating = False

'Save found addresses and text into arrays
On Error Resume Next
Set WB = ActiveWorkbook
If Err = 0 Then
On Error GoTo 0
For Each WS In WB.Worksheets
'Omit results page from search
If WS.Name <> "FindWord" Then
With WB.Sheets(WS.Name).Cells
Set Cell = .Find(What:=Search, LookIn:=xlValues, LookAt:=xlPart, _
MatchCase:=False, SearchOrder:=xlByColumns)
If Not Cell Is Nothing Then
FirstAddress = Cell.Address
Do
Counter = Counter + 1
ReDim Preserve FindCell(1 To Counter)
ReDim Preserve FindSheet(1 To Counter)
ReDim Preserve FindWorkBook(1 To Counter)
ReDim Preserve FindPath(1 To Counter)
ReDim Preserve FindText(1 To Counter)
ReDim Preserve FindRng(1 To Counter)
FindCell(Counter) = Cell.Address(False, False)
FindText(Counter) = Cell.Text
FindSheet(Counter) = WS.Name
FindWorkBook(Counter) = WB.Name
FindPath(Counter) = WB.FullName
FindRng(Counter) = Cell.Text
Set CopyRng = WS.Range("D1:E1")
Set Cell = .FindNext(Cell)
Loop While Not Cell Is Nothing And Cell.Address <> FirstAddress
End If
End With
End If
Next
End If
On Error GoTo 0
'Response if no text found
If Counter = 0 Then
MsgBox Search & " was not found.", vbInformation, "Zero Results For Search"
Exit Sub
End If

'Create FindWord sheet in does not exist
On Error Resume Next
Sheets("FindWord").Select
If Err <> 0 Then
Debug.Print Err
'error occured so clear it
Err.Clear
Sheets.Add.Name = "FindWord"
Sheets("FindWord").Move After:=Sheets(Sheets.Count)
'Run macro to add code to ThisWorkbook
AddSheetCode
End If
'Write hyperlinks and texts to FindWord
Range("A3:D65536").ClearContents
Range("A1:D1").Interior.ColorIndex = 6
Range("A1").Value = "Occurences of:"
'Reset prevents looping of code when sheet changes
If Reset = True Then Range("B1").Value = Search
Range("A1:D2").Font.Bold = True
Range("A2").Value = "Location"
Range("B2").Value = "Cell Text"
Range("C2").Value = "Name"
Range("D2").Value = "ID"
Range("A1:B1").HorizontalAlignment = xlLeft
Range("A2:D2").HorizontalAlignment = xlCenter
'Adjust column width to suit display
Range("A:A").ColumnWidth = ScreenWidth / 60
Range("B:B").ColumnWidth = ScreenWidth / 60
Range("C:C").ColumnWidth = ScreenWidth / 60
Range("D:D").ColumnWidth = ScreenWidth / 60

For Counter = 1 To UBound(FindCell)
ActiveSheet.Hyperlinks.Add Anchor:=Range("A" & Counter + 2), _
Address:="", SubAddress:=FindSheet(Counter) & "!" & FindCell(Counter), _
TextToDisplay:=FindSheet(Counter) & "!" & FindCell(Counter)
Range("B" & Counter + 2).Value = FindText(Counter)
Range("C" & Counter + 2).Value = CopyRng(Counter)

Next Counter
Range("B1").Select
Canceled:

Set WB = Nothing
Set WS = Nothing
Set Cell = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub


Sub AddSheetCode()
'Thanks to Dragontooth
Dim strCode As String
Dim FWord As String
Dim WB As Workbook
Dim Sh
Dim I As Integer
Set WB = ActiveWorkbook

'Line to be inserted instead of 4th line below if code in Personal.xls
'& "Application.Run (" & Chr(34) & "Personal.xls!Search.FindAll" & Chr(34) & "), Target.Text, " & Chr(34) & "False" & Chr(34) & vbCr _
'Optional 4th line if code in workbook
'& "FindAll Target.Text, " & Chr(34) & "False" & Chr(34) & vbCr _

strCode = "Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)" & vbCr _
& "If Sh.Name = " & Chr(34) & "FindWord" & Chr(34) & " Then" & vbCr _
& "If Target.Address = " & Chr(34) & "$B$1" & Chr(34) & " Then" & vbCr _
& "Application.Run (" & Chr(34) & "Personal.xls!Search.FindAll" & Chr(34) & "), Target.Text, " & Chr(34) & "False" & Chr(34) & vbCr _
& "End if" & vbCr _
& "End if" & vbCr _
& "End Sub"
'Debug.Print strCode

'Write code to ThisWorkbook module
FWord = "ThisWorkbook"
For I = 1 To WB.VBProject.VBComponents.Count
If WB.VBProject.VBComponents.Item(I).Name = FWord Then
Exit For
End If
Next
If Not WB.VBProject.VBComponents.Item(I).CodeModule Is Nothing Then
If Not WB.VBProject.VBComponents.Item(I).CodeModule.Find("Workbook_SheetChange", 1, 1, 100, 100) Then
WB.VBProject.VBComponents.Item(I).CodeModule.AddFromString (strCode)
End If
End If
Set WB = Nothing

End Sub



Again, any help is appreciated. :hi:

Many thanks :thumb:thumb

westconn1
06-23-2014, 02:50 PM
Set CopyRng = WS.Range("D1:E1")the range object for copyrng is over reset everytime in the loop, possibly this should be an array of ranges
as you indicate here
Range("C" & Counter + 2).Value = CopyRng(Counter)though, as is, this will return a cell value relative to copyrng of the last ws, but not necessarily within copyrng, depending on the value of counter

findrng appears to be an exact duplicate of findtext

Phlow
06-24-2014, 05:58 AM
Gotcha
amended it slightly and added an array:

Dim CopyRange As Range
Dim FindRange() As String


FindRng(Counter) = WS.Range("D1")
FindRange(Counter) = WS.Name
Set CopyRng = WS.Range("D1")

and:

Range("C" & Counter + 2).Value = FindRng(Counter)
Range("D" & Counter + 2).Value = FindRange(Counter)



Works perfect now.

Many thanks for the thoughts and help. Much appreciated