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

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)

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.

Nested if solution in Microsoft excel for partial matching strings

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

Add numbers from string values, excel

I have a table which has particular string values. SP-1, SP-2, SP-3,.. SP-8
and also V-4 and V-8. I want to add numbers present in the string. The string will be same (either SP- or V-). The numbers following the string will be different. The sum should be separate for each string type.
I have seen many solutions but not able to adapt them.
The table may contain empty cells. Hence I am unable to use Value function.
I want to check the entire table for all SP- strings and V- strings and have the sum of each type. I want to achieve this using formula and not macros. Can any of you help me with the formula
Use this array formula:
=SUM(IFERROR(--SUBSTITUTE($A$1:$A$6,C1&"-",""),0))
Being an array formula it needs be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
Try the following User Defined Function:
Public Function SpecialAdder(rng As Range, p As String) As Variant
Dim L As Long, r As Range
If p = "" Then
SpecialAdder = Application.WorksheetFunction.Sum(rng)
Exit Function
End If
SpecialAdder = 0
L = Len(p)
For Each r In rng
If Left(r.Value, L) = p Then
SpecialAdder = SpecialAdder + Mid(r.Value, L + 1)
End If
Next r
End Function
It would be used in the worksheet like:
=specialadder(A1:A100,"SP-")

How to format multi-line cell of numbers in Excel?

Given an Excel cell with multiple lines (text wrapping).
E.g.
5501.700
640.8690
1081.45600
41.100
I want to format all the numbers (in this single cell) to have 2 decimal places and thousand separators. What are my options to accomplish this task?
I think the only way is with a macro; read value, split each line, format, join.
ALT+F11, insert -> module and add;
Public Function FORMATLINES(cell As Range) As String
Dim data() As String, i As Long
data = Split(cell.Text, vbLf)
For i = 0 To UBound(data)
If IsNumeric(data(i)) Then data(i) = FormatNumber(data(i), 2, vbTrue, vbFalse, vbTrue)
Next
FORMATLINES = Join(data, vbLf)
End Function
For
=FORMATLINES(A1)
Gives me;
5,501.70
640.87
1,081.46
41.10

How to filter Excel columns values by Ms-excel formula?

I want to get filter column string from below Data.
URL PreFix OutPut ConcatStrings
────────────────────────────────────────────────────────────────────────────────────────────────────────────────
http://AbCD.com/grouponorange-county | Deals | orangecounty | Dealsorangecounty
In,
Column-1 first column there is an URL string.
Column-2 There is fixed words here ex. Deals or any fixed string
Column-3 Want to get string after this "http://AbCD.com/groupon" string
there is orange-Country then remove all special character so here output is orangeCountry.
Coulmn-4 ConcatString " [Column-2] + [Column-3] "
How I can do in Microsoft Excel sheet.
Column 4: use the formula =B:B&C:C.
Column 3: if you always have the same hostname, you can use:
=SUBSTITUTE(A:A,"http://AbCD.com/groupon","")
If it has a different hostname, use:
=RIGHT(A:A,LEN(A:A)-FIND("groupon",A:A)-LEN("groupon")+1)
(edit) Use Alt+F11 to go into VBA, go to Insert > Module, and paste this in:
Public Function remove_special_characters(s As String) As String
' based on code by Aaron Blood posted here:
' http://www.ozgrid.com/forum/showthread.php?t=55082&page=1
Dim cur_char As String
Dim i As Long
For i = 1 To Len(s)
cur_char = Mid(s, i, 1)
If cur_char Like "[A-Za-z0-9]" Then
remove_special_characters = remove_special_characters & cur_char
End If
Next i
End Function
You can then use =remove_special_characters() in Excel as a wrapper around the functions above.

Resources