Nested if solution in Microsoft excel for partial matching strings - excel

I am trying to write a formula for nested if in one of the columns in excel.
For ex:
I want to automatically get the values in one column based on the matching values in another column.
Lets say the two columns are A and B.
If A contains any string like "Bank" then i want the column B to be FGT_Bank_OSP.
If A contains any string like "PDM" then i want the column B to be "FGT_PDM_OSP.
Similarly i have 25 other values.
I tried to use something like this but tjis is not working.
=IF(ISNUMBER(SEARCH("DMT_Bank_Sensitive",E5)), "DMT_Bank_SEN_OSP", IF(ISNUMBER(SEARCH("DMT_PDM_Sensitive",E5)), "DMT_PDM_SEN_OSP"),"No")
Sample Data :Sample Data
Do we have any easy way or any function in excel?
Sample Data 2

Get Names UDF
In Excel e.g. for the value in A1 use it like this:
=getNames(A1)
or if case-insensitive (A=a,B=b) use:
=getNames(A1,1)
The Code
Option Explicit
Function getNames(SearchString As String, _
Optional CaseInSensitive1 As VbCompareMethod = 0) As String
Dim SearchNames, ReturnNames, i As Long
' Add more values when needed. This can also be written to
' retrieve values from a range.
SearchNames = Array("Bank", _
"CTAS", _
"PDM")
ReturnNames = Array("FDG_Bank_Material", _
"FDG_CTAS_Data", _
"FDG_PDM_Sensitive")
For i = 0 To UBound(SearchNames)
If InStr(1, SearchString, SearchNames(i), CaseInSensitive1) <> 0 Then
getNames = ReturnNames(i)
Exit For
End If
Next i
End Function

Related

Excel 2016 - Sort value in cell in ascending order

I currently have a cell with some values, separated by commas. I wish to sort them in ascending order.
Sample Input (Value in a single cell):
Sample Output (Value in a single cell):
I have seen many answers when it comes to sorting rows and columns, but I can't seem to sort the values in a single cell in ascending order. Is it possible to sort the values in a single cell in ascending order? Or is there a workaround for this?
Some explanation/documentation would be appreciated as I'm a beginner at VBA. Thank you for your help.
Please try to split data in a single cell by comma, then sorting it and combine all of them together?
The comments provide you with numerous ways to sort an array. Rightly or wrongly, I've provided my own flavour on an old VBA topic along with the extended piece of outputting the string in a delimited format. Take it or leave it ...!
You should be able to refer to the below function directly from any given cell like you would any built-in function in Excel.
Public Function SplitAndSortAscending(ByVal strText As String, ByVal strDelimiter As String) As String
Dim arrData() As String, arrNewData() As String, i As Long, x As Long, y As Long
arrData = Split(strText, strDelimiter)
ReDim arrNewData(UBound(arrData))
For i = 0 To UBound(arrData)
For x = 0 To UBound(arrNewData)
If arrData(i) < arrNewData(x) Or arrNewData(x) = "" Then
For y = UBound(arrNewData) To x + 1 Step -1
arrNewData(y) = arrNewData(y - 1)
Next
arrNewData(x) = arrData(i)
Exit For
End If
Next
Next
For i = 0 To UBound(arrNewData)
SplitAndSortAscending = SplitAndSortAscending & strDelimiter & arrNewData(i)
Next
SplitAndSortAscending = Mid(SplitAndSortAscending, Len(strDelimiter) + 1)
End Function
If you have O365, you can use something like the below to achieve the same sort of thing. Take note, my implementation will take 1.0 and format it as a whole number, i.e. it will come out as 1.
=TEXTJOIN(",",TRUE,SORT(FILTERXML("<r><v>" & SUBSTITUTE(A1,",","</v><v>") & "</v></r>","//v")))
The assumption is that the example you provided is in cell A1.

How to check if a substring is in any of the entries in rows of an excel sheet?

So I basically have a table with multiple columns.
So for each of the ids (1, 2, 3), I would like to check in which column there is sub-string of * (as you see sometimes it's in B and sometimes in C). Then I would like to extract the whole string that contains * and is associated with the given ID.
Suppose that my actual table contains over 10 columns - but the idea remains the same.
In other words the entries that I am looking for that contain a specific substring are scatted all throughout the 10 different columns.
Use HLOOKUP
=HLOOKUP("*~**",B1:C1,1,FALSE)
Since the asterisk(*) is a wildcard we need to append it with the tilde(~) to tell Search to look for the actual character.
The outer * allow the HLOOKUP to look at part.
Try the following User Defined Function:
Public Function FindTheStar(rng As Range) As String
Dim r As Range, v As String
FindTheStar = ""
For Each r In rng
v = r.Text
If InStr(v, "*") > 0 Then
FindTheStar = v
Exit Function
End If
Next r
End Function
It will find and return the first cell in any range that contains an asterisk.

Look up and return values from 2 columns all matches

I have List A and B in excel and would like to compare ALL the items in List A with ALL the records in List B and if they match or partial match return the value of B in 3rd column. Hopefully demonstrated in the attached.
example
The easiest way to achieve it is to use VBA. Please find below example function which you can use in the same way as Excel functions:
Public Function findArea(item As String, areaRng As Range) As String
Dim i As Long
Dim ARR_area() As Variant
ARR_area = areaRng.Value2
For i = LBound(ARR_area) To UBound(ARR_area)
If (item Like "*" & ARR_area(i, 1) & "*") Then
findArea = ARR_area(i, 1)
GoTo endFunc
End If
Next i
endFunc:
End Function
Where:
item - Item which you want to check vs. areas
area - range of areas you want to check.
See usage example:
To achieve this result without you would need to format table to pivot view, where in rows you would have item and in rows area - as the value you can check matching for each combination. Nevertheless in this particular example I would recommend to use VBA.
Hope it helped.

Prevent Partial Duplicates in Excel

I have a worksheet with products where the people in my office can add new positions. The problem we're running into is that the products have specifications but not everybody puts them in (or inputs them wrong).
Example:
"cool product 14C"
Is there a way to convert Data Valuation option so that it warns me now in case I put "very cool product 14B" or anything that contains an already existing string of characters (say, longer than 4), like "cool produKt 14C" but also "good product 15" and so on?
I know that I can prevent 100% matches using COUNTIF and spot words that start/end in the same way using LEFT/RIGHT but I need to spot partial matches within the entries as well.
Thanks a lot!
If you want to cover typo's, word wraps, figure permutations etc. maybe a SOUNDEX algorithm would suit to your problem. Here's an implementation for Excel ...
So if you insert this as a user defined function, and create a column =SOUNDEX(A1) for each product row, upon entry of a new product name you can filter for all product rows with same SOUNDEX value. You can further automate this by letting user enter the new name into a dialog form first, do the validation, present them a Combo Box dropdown with possible duplicates, etc. etc. etc.
edit:
small function to find parts of strings terminated by blanks in a range (in answer to your comment)
Function FindSplit(Arg As Range, LookRange As Range) As String
Dim LookFor() As String, LookCell As Range
Dim Idx As Long
LookFor = Split(Arg)
FindSplit = ""
For Idx = 0 To UBound(LookFor)
For Each LookCell In LookRange.Cells
If InStr(1, LookCell, LookFor(Idx)) <> 0 Then
If FindSplit <> "" Then FindSplit = FindSplit & ", "
FindSplit = FindSplit & LookFor(Idx) & ":" & LookCell.Row
End If
Next LookCell
Next Idx
If FindSplit = "" Then FindSplit = "Cool entry!"
End Function
This is a bit crude ... but what it does is the following
split a single cell argument in pieces and put it into an array --> split()
process each piece --> For Idx = ...
search another range for strings that contain the piece --> For Each ...
add piece and row number of cell where it was found into a result string
You can enter/copy this as a formula next to each cell input and know immediately if you've done a cool input or not.
Value of cell D8 is [asd:3, wer:4]
Note the use of absolute addressing in the start of lookup range; this way you can copy the formula well down.
edit 17-Mar-2015
further to comment Joanna 17-Mar-2015, if the search argument is part of the range you're scanning, e.g. =FINDSPLIT(C5; C1:C12) you want to make sure that the If Instr(...) doesn't hit if LookCell and LookFor(Idx) are really the same cell as this would create a false positive. So you would rewrite the statement to
...
...
If InStr(1, LookCell, LookFor(Idx)) <> 0 And _
Not (LookCell.Row = Arg.Row And LookCell.Column = Arg.Column) _
Then
hint
Do not use a complete column (e.g. $C:$C) as the second argument as the function tends to become very slow without further precautions

Reversing domain text in excel formula (split, reverse array, join)

Say I have a field in excel with a domain name and I would like to reverse the order of the subdomains, domain and tld, for sorting purposes. For example:
"my.sub.domain.example.org" becomes "org.example.domain.sub.my"
How would you do that in excel?
I'm not sure how you would do it with worksheet functions, creating a function to do it for you is a lot easier.
If you open the VBA editor, insert a new module and paste the following function you can use it on your worksheet.
Public Function Reverse(ByVal Expression As String, ByVal Delimiter As String) As String
Dim Data() As String
Dim Result As String
Dim Index As Integer
Result = ""
Data = Split(Expression, Delimiter)
Index = UBound(Data)
Result = Data(Index)
Do
Index = Index - 1
Result = Result & Delimiter & Data(Index)
Loop While Index > 0
Reverse = Result
End Function
Example
A1 ="my.example.site.tld"
A2 ="=Reverse(A1,".")"
A2=="tld.site.example.my"
Use text to columns with a period delimiter. then you can splice them together using =concatenate(...) or & (concatenation operand) in the order and format that you desire.
(but the VBA answer NickSlash posted is nice, this is just in case you want non-VBA)

Resources