I have the HighlighWords Macro below, that i used to search through a column and highlight all words found on "sheet1 column A", The script work normally if run from the macros window, I was trying to incorporate this code into a button on the ribbon bar to have it available at all times for different worksheets and i am running into a few hiccups and get and Run-time error 9 subscript out range error.
A little background
The script work normally if run from the macros window
I have modified the Excel Ribbon to display a custom tab which is stores in another xlsm file and launch at startup.
the file being modifide in another file
i have modified the code to work on the active worksheet an it runs but it highlights the entire cell. not just the words found in sheet 1 column A
Long term here to make it as dynamic as possible without hardcoding the source and destinationon cells. any input is great appreciated, thank you in advance.
A little background
The script work normally if run from the macros window
I have modified the Excel Ribbon to display a custom tab which is stores in another xlsm file and launch at startup.
the file being modifide in another file
VBA Code:
Sub HighlightWords()
Dim sourceSheet As Worksheet
Dim destinationSheet As Worksheet
Dim sourceCell As Range
Dim destinationCell As Range
Dim lastRowSource As Long
Dim lastRowDestination As Long
Dim text As String
Dim startPos As Integer
Dim endPos As Integer
' Set references to source and destination sheets
Set sourceSheet = ThisWorkbook.Sheets("Sheet1")
Set destinationSheet = ThisWorkbook.Sheets("SaveOn-MSTeams-0001")
' Find the last row with data in column A of Sheet1
lastRowSource = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
' Find the last row with data in column D of SaveOn-MSTeams-0001
lastRowDestination = destinationSheet.Cells(destinationSheet.Rows.Count, "D").End(xlUp).Row
' Loop through each cell in column A of Sheet1
For Each sourceCell In sourceSheet.Range("A1:A" & lastRowSource)
text = sourceCell.Value
' Loop through each cell in column D of SaveOn-MSTeams-0001
For Each destinationCell In destinationSheet.Range("D1:D" & lastRowDestination)
' Check if the destination cell contains the text from the source cell (wildcard matching)
startPos = InStr(1, LCase(destinationCell.Value), LCase(text), vbTextCompare)
If startPos > 0 Then
endPos = startPos + Len(text) - 1
' Highlight the matching phrase in the destination cell
destinationCell.Characters(startPos, Len(text)).Font.Color = RGB(255, 0, 0) ' Red color
destinationCell.Characters(startPos, Len(text)).Font.Bold = True ' Bold the selection
End If
Next destinationCell
Next sourceCell
End Sub
i have modified the code to work on the active worksheet an it runs but it highlights the entire cell. not just the words found in sheet 1 column A
Code:
Sub Macro8(control As IRibbonControl)
Dim sourceSheet As Worksheet
Dim destinationSheet As Worksheet
Dim sourceCell As Range
Dim destinationCell As Range
Dim lastRowSource As Long
Dim lastRowDestination As Long
Dim text As String
Dim startPos As Integer
Dim endPos As Integer
Set destinationSheet = ActiveSheet
Set sourceSheet = ThisWorkbook.Sheets("Sheet1")
lastRowSource = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
lastRowDestination = destinationSheet.Cells(destinationSheet.Rows.Count, "D").End(xlUp).Row
MsgBox "The name of the active sheet is " & ActiveSheet.Name
For Each sourceCell In sourceSheet.Range("A1:A" & lastRowSource)
text = sourceCell.Value
For Each destinationCell In destinationSheet.Range("D1:D" & lastRowDestination)
startPos = InStr(1, LCase(destinationCell.Value), LCase(text), vbTextCompare)
If startPos > 0 Then
endPos = startPos + Len(text) - 1
destinationCell.Characters(startPos, Len(text)).Font.Color = RGB(255, 0, 0) ' Red color
destinationCell.Characters(startPos, Len(text)).Font.Bold = True ' Bold the selection
End If
Next destinationCell
Next sourceCell
Long term here to make it as dynamic as possible without hardcoding the source and destinationon cells. any input is great appreciated, thank you in advance.