Adding code to worksheet module
The attachment is based on DRJ's extremely useful "Find all words in Excel" KB submission. For my purpose, I want to use it in a single workbook. The code as modified now adds a worksheet called "FindWord" to the active workbook, containing the information links etc. What I want to do is to add some code to this sheet, which will do another search when the search term in cell B1 is changed.
Problem is, I don't know much about adding VBComponents and referencing them:bawl
The attachment should run once, to show what should happen, but I'm having to reference FindWord as Sheet4, which may not always be true.
Secondly, I really want to put the search module into Personal.xls, so that I can call it from any spreadsheet. The further problem there is the "ActiveVBProject" (I think) which wants to write the code to a module in Personal.xls.
Any assistance greatly appreciated!
Regards,
MD
Code:
Sub ExportCodeMod()
Dim strCode As String
'Code to be written to "FindWord"
'Private Sub Worksheet_Change(ByVal Target As Range)
'If Target.address = "$B$1" Then
'Application.Run ("Personal.xls!Search.FindAll"), Target.Text, "False"
'Cells(1, 2).Select
'End if
'End Sub
'Line to be inserted instead of 3rd line below to run from Personal.xls
Code:
'& "Application.Run (" & Chr(34) & "Personal.xls!Search.FindAll" & Chr(34) & "), Target.Text, " & Chr(34) & "False" & Chr(34) & vbCr _
strCode = "Private Sub Worksheet_Change(ByVal Target As Range)" & vbCr _
& "If Target.Address = " & Chr(34) & "$B$1" & Chr(34) & " Then" & vbCr _
& "FindAll Target.Text, " & Chr(34) & "False" & Chr(34) & vbCr _
& "Cells(1,2).Select" & vbCr _
& "End if" & vbCr _
& "End Sub"
Application.VBE.ActiveVBProject.VBComponents.Item("Sheet4") _
.CodeModule.AddFromString (strCode)
End Sub