Help Please - How to search cell contents (text string) for specific set values but ignore others that contain the same entry – i.e. look for Rob but ignore if found within Robert
Hi All,
Been a while since I've needed any help as my current role doesn't demand much Excel use, however my lack of use have left my knowledge very rusty and as such I have hit a block with the this one and would appreciate your assistance with how to write the code and on how to make this code less bulky and more efficient (it's not slow, for my knowledge and learning).
I have attached an example sheet (if it works), but in summary: (edit: I can't seem to find how to attach - unless it's a video or picture)
I have column J & Y which contain text strings, of any length, structure etc - they are pretty much free type cells.
I want to search column J & Y to see if they contain some specific entries, which can be anywhere within the cell contents. There can be none of the ones I'm looking for, or there can be several within the cells text content.
The stuff I'm looking to find within the cell content is in the format:
1 digit Number, full stop, 1 digit number, full stop then either a 1 or 2 digit number. - e.g. 7.3.1 & 7.3.14
These references aren't terminated in a specific way, i.e. i can have 7.3.1, or 7.3.1R, or 7.3.1. etc.
When one of the specific entries that I'm looking for is found within either the J or Y column, then an 'X' is entered into another column AI:BE that corresponds to the reference it has found (obviously in the same row).
My aim is to search several hundred rows of data and end up with a matrix grid of X's so I can easily see which references have been found and also sort by these X's so I can retrieve the associated row data for further analysis.
All of the above I have achieved (and maybe not by the correct/best methods) by the following code. I should also point out that specific markers are placed in columns A,B & C, but these function fine, so just advising as way of explanation.
My problem is that I'm receiving false results, where I'm looking for a 3 digit reference that features within the 4 digits references. For example, if a cell contains only the 6.5.10 reference, then I get a positive X marker against 6.5.1 as well as 6.5.10, so get a false indication that the cell contains 6.5.1, when it doesn't.
Here’s my question:
How can I make it so that when it's checking for 6.5.1, it excludes 6.5.10? i.e. only counts 6.5.1 when the very next character isn't a number? As I think this would then rule out all the termination that can be after the 3rd number (6.5.1, 6.5.1R, 6.5.1 , etc - this next character can be anything, I just know it wont be a number, or it becomes a difference reference).
Not sure this makes sense, but what I see as the solution is (just can’t do it):
Search for 6.5.1 - if found, then check the next character, if it's a number then we haven't found 6.5.1 so ignore it - if the next character is not a number, then we have found it, so count it and apply the correct marker against 6.5.1.
Any help as to make this specific would be fantastic and if there is a way to write the below code better, please do tell. Just to note though, I need the text in each rows J & Y cells to be checked start to finish, for each reference, not just the first one found etc.
I’ve highlighted the ‘issue references’ below (but not sure if the highlight will pull through to the post), so here are the problem ones, as the others are unique and thus not found within others.
6.5.1
6.5.10
7.6.1
7.6.13
7.6.14
7.6.15
7.4.1
7.4.11
7.4.17
Many thanks all, and here's my fingers crossed!
Upex
Hi All,
Been a while since I've needed any help as my current role doesn't demand much Excel use, however my lack of use have left my knowledge very rusty and as such I have hit a block with the this one and would appreciate your assistance with how to write the code and on how to make this code less bulky and more efficient (it's not slow, for my knowledge and learning).
I have attached an example sheet (if it works), but in summary: (edit: I can't seem to find how to attach - unless it's a video or picture)
I have column J & Y which contain text strings, of any length, structure etc - they are pretty much free type cells.
I want to search column J & Y to see if they contain some specific entries, which can be anywhere within the cell contents. There can be none of the ones I'm looking for, or there can be several within the cells text content.
The stuff I'm looking to find within the cell content is in the format:
1 digit Number, full stop, 1 digit number, full stop then either a 1 or 2 digit number. - e.g. 7.3.1 & 7.3.14
These references aren't terminated in a specific way, i.e. i can have 7.3.1, or 7.3.1R, or 7.3.1. etc.
When one of the specific entries that I'm looking for is found within either the J or Y column, then an 'X' is entered into another column AI:BE that corresponds to the reference it has found (obviously in the same row).
My aim is to search several hundred rows of data and end up with a matrix grid of X's so I can easily see which references have been found and also sort by these X's so I can retrieve the associated row data for further analysis.
All of the above I have achieved (and maybe not by the correct/best methods) by the following code. I should also point out that specific markers are placed in columns A,B & C, but these function fine, so just advising as way of explanation.
My problem is that I'm receiving false results, where I'm looking for a 3 digit reference that features within the 4 digits references. For example, if a cell contains only the 6.5.10 reference, then I get a positive X marker against 6.5.1 as well as 6.5.10, so get a false indication that the cell contains 6.5.1, when it doesn't.
Here’s my question:
How can I make it so that when it's checking for 6.5.1, it excludes 6.5.10? i.e. only counts 6.5.1 when the very next character isn't a number? As I think this would then rule out all the termination that can be after the 3rd number (6.5.1, 6.5.1R, 6.5.1 , etc - this next character can be anything, I just know it wont be a number, or it becomes a difference reference).
Not sure this makes sense, but what I see as the solution is (just can’t do it):
Search for 6.5.1 - if found, then check the next character, if it's a number then we haven't found 6.5.1 so ignore it - if the next character is not a number, then we have found it, so count it and apply the correct marker against 6.5.1.
Any help as to make this specific would be fantastic and if there is a way to write the below code better, please do tell. Just to note though, I need the text in each rows J & Y cells to be checked start to finish, for each reference, not just the first one found etc.
I’ve highlighted the ‘issue references’ below (but not sure if the highlight will pull through to the post), so here are the problem ones, as the others are unique and thus not found within others.
6.5.1
6.5.10
7.6.1
7.6.13
7.6.14
7.6.15
7.4.1
7.4.11
7.4.17
Many thanks all, and here's my fingers crossed!
Upex
Code:
Sub highlight_Notifiable_CASS_rows_Mark_Up_columnB_and_rule_indicators()
Dim lngI As Long
For lngI = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row
If InStr(1, Range("J" & lngI).Value, "6.5.1", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("aj" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "6.5.2", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ak" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "6.5.6", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("al" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "6.5.10", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("am" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.6.1", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("an" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.6.2", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ao" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.6.9", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ap" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.6.13", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("aq" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.6.14", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ar" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.6.15", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("as" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "6.5.1", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("aj" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "6.5.2", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ak" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "6.5.6", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("al" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "6.5.10", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("am" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.6.1", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("an" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.6.2", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ao" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.6.9", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ap" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.6.13", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("aq" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.6.14", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("ar" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.6.15", vbTextCompare) Then
Range("B" & lngI).Value = "Yes"
Range("as" & lngI).Value = "X"
End If
Next lngI
End Sub
Sub highlight_Non_Notifiable_CASS_rows_Mark_Up_columnC()
Dim lngI As Long
For lngI = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row
If InStr(1, Range("J" & lngI).Value, "7.2.8", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("av" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.2.9", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("aw" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.2.17", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("ax" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.3.1", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("ay" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.4.1", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("az" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.4.11", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("ba" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.4.17", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bb" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.4.22", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bc" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "7.4.25", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bd" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "8.1.5", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("be" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "8.3.1", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bf" & lngI).Value = "X"
End If
If InStr(1, Range("J" & lngI).Value, "8.3.2", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bg" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.2.8", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("av" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.2.9", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("aw" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.2.17", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("ax" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.3.1", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("ay" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.4.1", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("az" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.4.11", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("ba" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.4.17", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bb" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.4.22", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bc" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "7.4.25", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bd" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "8.1.5", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("be" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "8.3.1", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bf" & lngI).Value = "X"
End If
If InStr(1, Range("Y" & lngI).Value, "8.3.2", vbTextCompare) Then
Range("C" & lngI).Value = "Yes"
Range("bg" & lngI).Value = "X"
End If
Next lngI
End Sub
Last edited: