I'm currently stuck with my VBA code to get the precise part of the string highlighted.
The ask: I need to identify within the string where TEMPO and TSRA are and change the font to bold red for everything in between them(including them as well).
The problem: There are many times where TEMPO can be included in the field, but I only want the TEMPO directly before an instance of TSRA, not the first instance of TEMPO through the entire message to the end of TSRA. For this problem I've tried using RegEx to identify but end up getting more than needed highlighted.
The secondary problem is that in some cases there can be multiple occurrences of TEMPO and TSRA in the same line and I need all of them also identified, not just the first instance of it. For this problem I have used position indicators to highlight, but only return the first instance of TEMPO to TSRA.
Below is an example of a string I'm testing: TAF USTR 191357Z 1915/2015 13005G13MPS 6000 FEW006 BKN020CB TEMPO1915/1918 VRB20MPS 0600 +TSRAGR SQ VV003 TEMPO 1918/200214003G11MPS 0200 TSRA FG VV002 FM200300 22005G12MPS 9999 BKN020CBTEMPO 2009/2015 24014MPS 3100 -TSRA
I tried using just position indicators in a loop with this code:
Sub HighlightTSRATerms()
Dim targetRange As Range
Dim cell As Range
Dim cellText As String
Dim startPos As Long
Dim endPos As Long
Dim highlightLength As Long
Dim regEx As Object
Dim match As Object
Dim matches As Object
Dim vctsPos As Long
' Set the target range
Set targetRange = ActiveSheet.Range("N1:N100")
' Create RegExp object for ####/ pattern
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "\b\d{4}/"
regEx.Global = True
regEx.IgnoreCase = True
regEx.MultiLine = True
' Loop through each cell
For Each cell In targetRange
If Not IsEmpty(cell.Value) Then
cellText = CStr(cell.Value)
' === Highlight "TEMPO" to "TSRA" ===
startPos = InStr(1, cellText, "TEMPO", vbTextCompare)
If startPos > 0 Then
endPos = InStr(startPos + Len("TEMPO"), cellText, "TSRA", vbTextCompare)
If endPos > 0 Then
highlightLength = (endPos + Len("TSRA")) - startPos
If startPos + highlightLength - 1 <= Len(cellText) Then
On Error Resume Next
With cell.Characters(Start:=startPos, Length:=highlightLength).Font
.Color = RGB(255, 0, 0)
.Bold = True
End With
On Error GoTo 0
End If
End If
End If
End If
Next cell
End Sub
This code only highlights the first instance of TEMPO to TSRA, not every occurrence Yellow highlighted needs to be included as well. (https://i.sstatic.net/fzGgmLM6.png)
Then I moved onto a RegEx method using this code:
Sub HighlightTSRATerms()
Dim targetRange As Range
Dim cell As Range
Dim cellText As String
Dim startPos As Long
Dim endPos As Long
Dim highlightLength As Long
Dim regEx As Object
Dim match As Object
Dim matches As Object
Dim vctsPos As Long
' Set the target range
Set targetRange = ActiveSheet.Range("N1:N100")
' Create RegExp object for ####/ pattern
Set regEx2 = CreateObject("VBScript.RegExp")
regEx2.Pattern = "\b\d{4}/"
regEx2.Global = True
regEx2.IgnoreCase = True
' Create RegExp object for TEMPO through TSRA
Set regEx = CreateObject("VBScript.RegExp")
regEx.Pattern = "\b\TEMPO.*[^\n]\TSRA"
regEx.Global = True
regEx.IgnoreCase = True
regEx.MultiLine = True
' Loop through each cell
For Each cell In targetRange
If Not IsEmpty(cell.Value) Then
cellText = CStr(cell.Value)
' === Highlight "TEMPO" to "TSRA" ===
If regEx.test(cellText) Then
Set matches = regEx.Execute(cellText)
For Each match In matches
If match.FirstIndex + match.Length <= Len(cellText) Then
On Error Resume Next
With cell.Characters(Start:=match.FirstIndex + 1, Length:=match.Length).Font
.Color = RGB(255, 0, 0)
.Bold = True
End With
On Error GoTo 0
End If
Next match
End If
End If
Next cell
End Sub
This code highlights everything from the first instance of TEMPO to the last TSRA but includes everything, which includes text I don't want highlighted The yellow highlight here shows the parts of the string I don't want in red (https://i.sstatic.net/rUj285lk.png)
This is the expected output that I desire: (https://i.sstatic.net/itFdoZrj.png)
Any help if greatly appreciated!

TEMPO(?:(?:(?!TSRA).)*TSRA)?|TSRAregex101.com/r/sa5XL0/1