Dear all
I have written the below code that looks for two types of codes; a NIU code abd CAR code within the text of the activecell.
What I wish to add to this code is the ability once a NIU or CAR is found is for the code to be changed to red within the activecell.
It is important to note that there may be zero or many of these to change within the activecell.
Is this possible to do? Thanks in advance for your thoughts.
Jim
I have written the below code that looks for two types of codes; a NIU code abd CAR code within the text of the activecell.
What I wish to add to this code is the ability once a NIU or CAR is found is for the code to be changed to red within the activecell.
It is important to note that there may be zero or many of these to change within the activecell.
Is this possible to do? Thanks in advance for your thoughts.
Jim
Code:
Sub Macro1()
'
' Macro1 Macro
'
'
Dim Pos As Variant
Dim str As Variant
Dim comparison1 As Variant
Dim comparison2 As Variant
Dim NIU As Variant
Dim CAR As Variant
Dim end_Pos As Variant
Dim Start_Pos As Variant
comparison1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
comparison2 = "0123456789"
Do
str = UCase(ActiveCell)
NIU = 0
CAR = 0
Pos = 1
end_Pos = Len(str)
'searches for NIU code in format of letter, 4 numbers, letter eg K1435G
Do Until end_Pos < 6 Or end_Pos - Pos = 5
If InStr(1, comparison1, Mid(str, Pos, 1), vbTextCompare) > 0 Then
If InStr(1, comparison2, Mid(str, (Pos + 1), 1), vbTextCompare) > 0 Then
If InStr(1, comparison2, Mid(str, (Pos + 2), 1), vbTextCompare) > 0 Then
If InStr(1, comparison2, Mid(str, (Pos + 3), 1), vbTextCompare) > 0 Then
If InStr(1, comparison2, Mid(str, (Pos + 4), 1), vbTextCompare) > 0 Then
If InStr(1, comparison1, Mid(str, Pos + 5, 1), vbTextCompare) > 0 Then
NIU = NIU + 1
End If
End If
End If
End If
End If
End If
Pos = Pos + 1
Loop
Pos = 1
'searches for CAR code in format of 3 letters, 3 numbers, eg KSA143
Do Until end_Pos < 6 Or end_Pos - Pos = 5
If InStr(1, comparison1, Mid(str, Pos, 1), vbTextCompare) > 0 Then
If InStr(1, comparison1, Mid(str, (Pos + 1), 1), vbTextCompare) > 0 Then
If InStr(1, comparison1, Mid(str, (Pos + 2), 1), vbTextCompare) > 0 Then
If InStr(1, comparison2, Mid(str, (Pos + 3), 1), vbTextCompare) > 0 Then
If InStr(1, comparison2, Mid(str, (Pos + 4), 1), vbTextCompare) > 0 Then
If InStr(1, comparison2, Mid(str, Pos + 5, 1), vbTextCompare) > 0 Then
CAR = CAR + 1
End If
End If
End If
End If
End If
End If
Pos = Pos + 1
Loop
If NIU + CAR = 0 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ElseIf NIU = 1 And CAR = 1 Then
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65280
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Else
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
ActiveCell.Offset(1, 0).Select
Loop Until IsEmpty(ActiveCell) = True
Last edited by a moderator: