Home » excel » Searching and returning for "*1*" in a string returns instances containing "*11*" as well in excel

# Searching and returning for "*1*" in a string returns instances containing "*11*" as well in excel

Questions:

I am attempting to extract cells through a combination of index(match) and right(len)-find() functions from an array of data with text. In my formula, I am searching for instances of “* DS#1 “, excel returns those but also returns instances with “ DS#11 *”. How do I get excel to return only DS#1?

I have attempted to use an if statement with no success, if(formula=”* 11 *”,””,formula).

Below is a link to an example of the data. The first cell highlighted in yellow should not be returning that text, it should be “”. The second cell highlighted in yellow is appropriate to return that data.
example data

=RIGHT(INDEX(\$V:\$AC,MATCH(“DS#1”,\$AC:\$AC,0),1),LEN(INDEX(\$V:\$AC,MATCH(FW\$1,\$AC:\$AC,0),1))-FIND(\$AG2,INDEX(\$V:\$AC,MATCH(FW\$1,\$AC:\$AC,0),1))+1)

Here some example on how to find a value and check for the following char.

Formula in `D2`:

``````=INDEX(A2:A6,MATCH(1,INDEX((ISNUMBER(SEARCH("DS#1",B2:B6)))*(NOT(ISNUMBER(MID(B2:B6,SEARCH(C2,B2:B6)+LEN(C2),1)*1))),0),0))
``````

Here is a formula you can adapt to your ranges which will return a list from the range `rngDS` that contain `findDS`. I used named ranges, but you can adapt them to your own ranges.

Not sure if this is what you want since you chose to not post examples of your data or desired results.

The routine finds the `findDS` string and then checks to be sure that the following character is non-numeric.

``````C1: =IFERROR(INDEX(rngDS,AGGREGATE(15,6,1/(NOT(ISNUMBER(-MID(rngDS,SEARCH(findDS,rngDS)+LEN(findDS),1))+ISERROR(MID(rngDS,SEARCH(findDS,rngDS)+LEN(findDS),1))))*ROW(rngDS),ROWS(\$1:1))),"")
``````

and fill down

It would be very difficult to come up a formula based solution especially when you need to first differentiate DS1, DS#1. DS#11, DS#11X etc. then look for the text string after each DS code, not to mention these confusing codes may (or may not) be positioned in random orders in the text string.

A better approach would be using Power Query which is available in Excel 2010 and later versions. My solution is using Excel 2016.

Presume you have the following two tables:

You can use From Table function in the Data tab to add both tables to the Power Query Editor.

Once added, make a duplicate copy of Table 1. I have renamed the duplicate as `Table1 (2) - Number Ref`. Then you should have three un-edited queries:

If your source data is a larger table containing some other information, you can google how to add a worksheet to the editor and how to remove unnecessary columns and remove duplicated values.

Firstly, let’s start working with Table1.

Here are the steps:

1. Use Replace Values function to remove all `#` from the text string, and then replace all `DS` with `DS#` in the text string, so all DS codes are in the format of `DS#XXX`. Eg. DS8 will be changed to DS#8. This step may not be necessary if DS8 is a valid code as well as DS#8;
2. Use Split Column function to split the text strings by the word `DS`, and put each sub text string into a new row, then you should have the following:

1. Use Split Column function again to split the text strings by 1 Character from the left and you should have the following:

1. Filter the first column to show hash tag `#` only and then remove the first column, then you should have the following:

1. use Replace Values function repeatedly to remove the following characters/symbols from the text strings: `(`, `)`, `HT`, `JH`, `SK`, `//`, and replace dash `-` with space . I presume these are irrelevant in the comment but you can leave them if needed. Then you should have:

1. use Split Column function again to split the text string by the first space on the left, then you should have:

1. Then you can Trim and Clean the second column to further tidy up the comments, rename the columns as DS#, Comments, and Number Ref consecutively, and change the format of the third column to Text. Then you should have:

1. The last step is to add a custom column called `Match ID` to combine the value from first and third column into one text string as shown below:

Secondly, let’s work on Table1 (2) – Number Ref

Here are the steps:

1. Remove the first column so leave the `Number Ref` column as the single column;
2. Transpose the column, and Promote the first row as header. Then you should have:

The purpose of this query is to transform all Number Reference into column headers, then append this query with the next query (Table2) to achieve the desired result which I will explain in the next section.

Lastly, let’s work on the third query Table2.

Here are the steps:

1. Append this table with the Number Ref table from previous step;

1. highlight the whole table and use Replace Values function to replace all `null` with number `1`. Then highlight the first column and use Unpivot Other Columns function to transform the table as below:

1. then Remove the last column (`Value`), and add a new custom column called `Match ID` to combine the DS code with the Number Reference. Then you should have:

1. Merge the table with Table1 using `Match ID` as shown below:

1. Expand the newly merged column `Table1` to show Comments;

1. Use Split Column function to split the first column by Non-digit to digit, change the format of the `digit column` to whole number, and then sort `Attribute` column and the `digit column` ascending consecutively, then you should have:

1. Use Split Column function again to split the `Match ID` column by dash sign `-`, and remove the first three columns, rename the remaining three columns as DS#, Number Ref and Comments consecutively, then you should have:

1. Close & Load this table to a new worksheet as desired, which may look like this:

In conclusion, It is entirely up to you how you would like to structure the table in Power Query. You can pre-filter the Number Reference in the editor and load only relevant results to a worksheet, you can load the full table to a worksheet and use VLOOKUP or INDEX to retrieve the data as desired, or you can load the third query to data model from where you can create pivot tables to play around.

Here are the codes behind the scene for reference only. All steps are using built-in functions of the editor without any advanced manual coding.

Table1

``````let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Comments", type text}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Replaced Value9", {{"Comments", Splitter.SplitTextByDelimiter("DS", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Comments"),
#"Filtered Rows" = Table.SelectRows(#"Split Column by Position", each ([Comments.1] = "#")),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","-"," ",Replacer.ReplaceText,{"Comments.2"}),
#"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter1",{{"Comments.2.2", Text.Trim, type text}}),
#"Cleaned Text" = Table.TransformColumns(#"Trimmed Text",{{"Comments.2.2", Text.Clean, type text}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Number Ref", type text}}),
in
``````

Table1 (2) – Number Ref

``````let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Comments", type text}, {"Number Ref", Int64.Type}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"Number Ref"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Other Columns",{{"Number Ref", type text}}),
#"Transposed Table" = Table.Transpose(#"Changed Type1"),
#"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers",{{"388", type any}, {"1", type any}})
in
#"Changed Type2"
``````

Table2

``````let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"List", type text}}),
#"Appended Query" = Table.Combine({#"Changed Type", #"Table1 (2) - Number Ref"}),
#"Replaced Value" = Table.ReplaceValue(#"Appended Query",null,"1",Replacer.ReplaceValue,{"List", "388", "1"}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Replaced Value", {"List"}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Value"}),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Match ID"}, Table1, {"Match ID"}, "Table1", JoinKind.LeftOuter),
#"Split Column by Character Transition" = Table.SplitColumn(#"Expanded Table1", "List", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"List.1", "List.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Character Transition",{{"List.2", Int64.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Attribute", Order.Ascending}, {"List.2", Order.Ascending}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Sorted Rows", "Match ID", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Match ID.1", "Match ID.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"List.1", type text}, {"Match ID.1", type text}, {"Match ID.2", type text}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type2",{"Match ID.1", "Match ID.2", "Comments"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{"Match ID.1", "DS#"}, {"Match ID.2", "Number Ref"}})
in
#"Renamed Columns"
``````

Cheers 🙂

Based on the creative answers from this post, I was able to take some of these ideas and form a solution. There are two parts to the solution I found. The first is to replace the strings that I am trying to filter out with a random text that doesn’t appear in any instance in my data. Since I had a range of data I needed to replace (DS11 through DS19), I used a VBA function to avoid a large nested function.

Once I had the strings I am trying to filter out replaced, I added an If(Isnumber(search()) function to display “” when the replaced text is returned.

``````Function REPLACETEXTS(strInput As String, rngFind As Range, rngReplace As Range) As String

Dim strTemp As String
Dim strFind As String
Dim strReplace As String

Dim cellFind As Range

Dim lngColFind As Long
Dim lngRowFind As Long
Dim lngRowReplace As Long
Dim lngColReplace As Long

lngColFind = rngFind.Columns.Count
lngRowFind = rngFind.Rows.Count
lngColReplace = rngFind.Columns.Count
lngRowReplace = rngFind.Rows.Count

strTemp = strInput

If Not ((lngColFind = lngColReplace) And (lngRowFind = lngRowReplace)) Then
REPLACETEXTS = CVErr(xlErrNA)
Exit Function
End If

For Each cellFind In rngFind

strFind = cellFind.Value
strReplace = rngReplace(cellFind.Row - rngFind.Row + 1, cellFind.Column - rngFind.Column + 1).Value
strTemp = Replace(strTemp, strFind, strReplace)

Next cellFind

REPLACETEXTS = strTemp

End Function
``````