How to split by multiple delimiters in vba excel - excel

I need to split some data with multi delimiters (//,/,-) , and I used one cell (A3) as data entry cell and I need multi delimiters to provide multi option to the user.
and I also need to know the availability to re-arrange the splitting results like if the results involved words content (*.com or *.net) transfer to certain column
I try to use a code to split but it is working with one delimiter

Make them the same.
Say we have a string that we want to parse by both / and $. Here an example:
Sub multiparse()
Dim s As String, s2 As String
s = "poiuy/tyuiop$7654$lkiop/"
s2 = Replace(s, "/", "$")
arr = Split(s2, "$")
End Sub

Related

line splitting using patterns

My situation is this:
I have a workbook in which i take the addresses from column L and paste them into another workbook in column Y
The addresses are like this:
Analipseos, Katerinis, 60500
Ermioni, Ermioni, 21051, Alkistis, Alkistis, 21052
Agia, Agia, 40003, skiathos, skiathos 37002
I want to split each line every time there's a comma ,
Is there a way to split the first three for example taking from Agia, Agia, 40003, skiathos, skiathos 37002 to split Agia, Agia, 40003 and the rest skiathos, skiathos 37002 to be pasted elsewhere for example in column B.
Note: I don't want the commas to be pasted anywhere.
Could anyone help?
I tried this code in my loop: Sub tst() Application.DisplayAlerts = False Range("A2", Range("A" & Rows.Count).End(xlUp)).TextToColumns Range("B2"), xlDelimited, , , , , True Application.DisplayAlerts = True
End Sub
In a separate sheet it works just fine, but in my code loop it doesn' work properly
Since you want to split on the 3rd comma, you can go about it in a slightly different way:
Simply replace the 3rd comma by a character you know won't appear in the text otherwise, for example, a | character.
Excel makes this very easy to do using the SUBSTITUTE function.
So, for example, suppose you have the text Agia, Agia, 40003, skiathos, skiathos 37002 in cell L1, you can replace the 3rd comma to a | character using the following function:
=SUBSTITUTE(L1,",","|",3)
Making the new (substituted) text become: Agia, Agia, 40003| skiathos, skiathos 37002
Now, you can:
Replace all the other commas if you no longer want them
Use Data > Text to columns and split based upon the new | character which will only appear where you want it.
I hope that does the trick!

Macro that sorts call signs containing letters and numbers

All the call signs will be in column A and when the macro is run should sort them. The sort is case insensitive usually in all caps. A call sign consists of 1-2 letters(prefix), 1-2 numbers(numbers), then 1-3 letters(suffix) I want to sort each sign by the number, suffix, then prefix in that order.
W9K, BB3C, W9GFO, AB8VN, G3G, A77Bc, KB8HTM, K9DOG, W8AER, K1ZZ, W4BFT, W0CQC, WA6FV, W6TRW, AA5B, W4IY, N4C, K5UZ, K4LRG
I will bite. Half the fun of coding is solving a problem for the simple pleasure of knowing you figured it out.
Here is a user defined function (Formula) that you can use to convert the call sign into the format for sorting. Note the numeric portion is zero padded so ones and tens do not sort together before twos and twenties.
Option Explicit
Public Function FormatCallSign(aCell As Range)
Dim Nbr As String
Dim i As Integer
Dim tmp As String
Dim vList As Variant
For i = 1 To Len(aCell.Value)
If InStr(1, "1234567890", UCase(Mid(aCell.Value, i, 1))) > 0 Then
Nbr = Nbr & Mid(aCell.Value, i, 1)
tmp = tmp & ","
tmp = Replace(tmp, ",,", ",")
Else
If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", UCase(Mid(aCell.Value, i, 1))) > 0 Then
tmp = tmp & Mid(aCell.Value, i, 1)
End If
End If
Next i
vList = Split(tmp, ",")
FormatCallSign = vList(1) & Right("0" & Nbr, 2) & vList(0)
End Function
Put the formula in cell B2, for example by using the formulas command on the ribbon and selecting the function from the user defined section.
As asked earlier if the call sign had delimiters in it already, you could use a simple formula to rearrange the parts and exclude the delimiters.
=CONCATENATE(MID(A3,SEARCH("-",A3)+1,4),RIGHT("0"&MID(A3,SEARCH("/",A3)+1,SEARCH("-",A3)-SEARCH("/",A3)-1),2),LEFT(A3,SEARCH("/",A3)-1))
To build a formula like the above, start by constructing it in parts.
First write a Search function to find the "/", then copy it to find the "-"
Then write a mid function to get the characters to the right of the dash, left of the slash, then the numeric section. paste the formulas into a single formula for your masterpiece.
Since it makes better sense to keep the three elements in separate fields for simplified sorting, the above formula can be split into three separate formulas, one for each column.
=MID(A3,SEARCH("-",A3)+1,4)
=value(MID(A3,SEARCH("/",A3)+1,SEARCH("-",A3)-SEARCH("/",A3)-1),2))
=LEFT(A3,SEARCH("/",A3)-1)
This corrects sorting problems given the three elements are variable length.
The initial specification for callsign format is inaccurate, since they can begin with numbers or letters and a logical sort would be by ITU assigned prefix. A function would need a table lookup for country after it determined if the string after the forward slash was a valid country designation. This is actually a pretty complicated problem.

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)

How to parse in VBA a string that contains Mathematics special ASCII code and correct it

I'm currently writing a code in VBA to retrieve prices for some financial models. The problem I have is in the excel spreadsheet where I have special Ascii characters like ⅞ ¼ etc...
I would need using VBA to transform this in 7/8 1/4 etc...
How could I do that ?
Thanks for your help
If you want literal string replacement, use the Replace function thus:
Sub changeit()
Dim w As Worksheet
Dim r As Range
For Each r In Application.Selection
r.Value = Replace(r.Value, Chr$(188), "1/4")
r.Value = Replace(r.Value, Chr$(189), "1/2")
r.Value = Replace(r.Value, Chr$(190), "3/4")
Next
End Sub
et cetera. (I have done each one separately to make it easier to read.) Alternatively, you could replace the literal strings "1/4" as #jalexiou suggests with 0.25 etc., ideally using a lookup table. Not sure what the Chr$ code is for the seven-eighths though.
Create a lookup table. When it finds one of these special characters set the value appropriately (example "1/2" = 0.5) and if not use the Val() function to get the value.

Resources