Using Split To Separate only 1st instance-VBA - excel

I have a situation where I need to separate a statement pulled from SAP based on the word "Caller" The issue is that this word is used multiple times throughout the statement but I need it to only cut off the 1st sentence leading up to the 1st "Caller". Is there a way to have split separate these words out and then recombine everything split except the (0) instance.
Here is my code that I am using right now.
Description_Rough = session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/cntlTEXT/shellcont/shell").Text
Descrption_Final = Split(Description_Rough, "Caller")(1)
And the statement being pulled as description_rough is
08.08.2018 21:53:55 UTC "Name" (Numbers) Phone #########
17.07.2018 16:25:47 AAAAAA AAAAAA (AAAAAA)
Caller is patient using device. Caller reports that she has been using
current device for about 9 days and that the problems arose with the device. Caller reports that she monitors the device
each dose and does not recall what dose was in the device before it went to all red. Patient to return affected device to
Company; mail order pharmacy to replace to patient; Company to replace to
Pharmacy.
What I am looking for is everything beginning at "Caller is patient using device."

Simplest way to do it in my opinion
Sub Sample()
Dim Descrption_Final As String
Dim pos As Long
Descrption_Final = "17.07.2018 16:25:47 AAAAAA AAAAAA (AAAAAA) Caller is patient Blah Blah....."
pos = InStr(1, Descrption_Final, "caller", vbTextCompare)
If pos > 0 Then Debug.Print Mid(Descrption_Final, pos)
'~~> Output: Caller is patient Blah Blah.....
End Sub
And if you want everything before Caller is patient Blah Blah..... then use 0 instead of 1
Split(Description_Rough, "Caller")(0)
Example
Sub Sample()
Dim Descrption_Final As String
Dim pos As Long
Descrption_Final = "17.07.2018 16:25:47 AAAAAA AAAAAA (AAAAAA) Caller is patient Blah Blah....."
Debug.Print Split(Descrption_Final, "Caller")(0)
'~~> Output: 17.07.2018 16:25:47 AAAAAA AAAAAA (AAAAAA)
End Sub

This should work. It just starts from the second occurrence of "Caller" and pieces it all back together from there.
Description_Rough = "Caller is patient using device. Caller reports that..."
Dim temp As Variant
temp = Split(Description_Rough, "Caller")
Dim Description_Final As String
Dim i As Long
For i = LBound(temp) + 1 To UBound(temp)
Description_Final = Description_Final & "Caller" & temp(i)
Next i

Related

Reorder parts of a name with a suffix

I need help on vba code to reorder the parts of a name. Sometimes there is a suffix (Jr, Sr, I, II, III, IV), and that is the part I can't figure out. There is no list that I need to loop thru. The elements of the name could look like this: Johnson, Joseph Allen Jr
This code works for getting the last name moved to the end, but now I need to trim & move the suffix to the right after the last name.
Range("A1") = Trim(StrReverse(Split(StrReverse(Range("A1")), ",")(0)) & " " _
& StrReverse(Split(StrReverse(Range("A1")), ",")(1)))
Result: Joseph Allen Jr Johnson
Result Required: Joseph Allen Johnson Jr
Thanks for any help!
So if your inputs are like Johnson, Joseph Allen Jr then you could set up an array/collection with the suffixes you want to check against. Then before you move the last name use
Dim i as Variant
Dim suffixArray as String()
Dim nameString as String
Dim suffixString as String
Dim arrayEntryLength as Long
suffixArray(0) = "Jr"
suffixArray(1) = "Sr"
suffixArray(2) = "I"
suffixArray(3) = "II"
...
for each i in suffixArray
nameString = "Johnson, Joseph Allen Jr" 'or use Cells(1,"a").value
arrayEntryLength = len(suffixArray(i))
if Right(nameString, arrayEntryLength) = suffixArray(i) then
suffixString = suffixArray(i)
nameString = left(nameString, len(nameString)-arrayEntryLength)
end if
next i
'move the last name of nameString
nameString = nameString & " " & suffixString
'rest of code
The full name is a unparsed string and manipulating "Suffix" as mentioned above by Chris H may be a suitable way. Here is Fullname content model in Outlook contact Item: https://learn.microsoft.com/en-us/office/vba/api/outlook.contactitem.fullname. Building an array of known suffixes will solve this in general. Watch out: the suffix may such as King Louis XVII, etc. is some historical context.

How to find values that aren't 100% duplicated?

I have a list of people getting aid from my organization.
Some register their names with different spelling or different order.
Here is an example
**Names** **ID**
Ahmed mohammed Saleh 3576158946 Personal ID
Waleed Khalid Ali 5478698645 Personal ID
Fatima Nader Aljalal 4684325986 Personal ID
Hussan Huessien Ahmed 778569 Family ID
*Ahmed Mohamed Salah* 698745 Family ID
*Waleed Ali Khalid* No ID
The last two in the list have registered twice.
My data has 4000 rows and I have to find the partial duplicates.
One way to reduce the difficulty of the task is to hash the names to a sorted string of lowercase characters with duplicate characters, spaces and vowels removed. You could then compare the hashed names to determine similarity. In the example below we are fortunate that we get exact matches but it would not be impossible to write a further function that checked if the hashed names differed by one, two or more characters, and in fact that the original names were a reasonable match.
Option Explicit
Private Type State
CharArray As Variant
End Type
Private s As State
Public Sub test()
Initialise
Debug.Print "Ahmed mohammed Saleh", ConvertNameToHash("Ahmed mohammed Saleh")
Debug.Print "Ahmed Mohamed Salah", ConvertNameToHash("Ahmed Mohamed Salah")
Debug.Print "Waleed Khalid Ali", ConvertNameToHash("Waleed Khalid Ali")
Debug.Print "Waleed Ali Khalid", ConvertNameToHash("Waleed Ali Khalid")
End Sub
Public Sub Initialise()
s.CharArray = Split("b,c,d,f,g,h,j,k,l,m,n,p,q,r,s,t,v,w,x,y,z", ",")
End Sub
Public Function ConvertNameToHash(ByVal ipName As String) As String
Dim myChars As String
Dim myName As String
myName = LCase$(ipName)
Dim myChar As Variant
For Each myChar In s.CharArray
If InStr(myName, myChar) > 0 Then
myChars = myChars & myChar
End If
Next
ConvertNameToHash = myChars
End Function
The output from the above code was
Ahmed mohammed Saleh dhlms
Ahmed Mohamed Salah dhlms
Waleed Khalid Ali dhklw
Waleed Ali Khalid dhklw

regex for Excel to remove all but specific symbols after a specific symbol?

I have stings like this which are addresses, e.g.:
P.O. Box 422, E-commerce park<br>Vredenberg<br><br><br>Curaçao
Adelgatan 21<br>Malmö<br><br>211 22<br>Sweden
Läntinen Pitkäkatu 35 A 15<br>Turku<br><br>20100<br>Finland
I am interested in Country only. Country always comes last after a <br> tag.
Note, that there can be several such tags preceding this last value (e.g. 1st example string).
Is there a good way to do a formula may ve along those lines:
Identify end of string
Loop a character back until one reaches ">" character
Cut everything else (including the ">" encountered)
You don't need RegEx to do this if it's always the last part of the string.
You can get it with String modifiers doing
Sub Test()
Dim str As String, str1 As String, str2 As String
Dim Countries As String
str = "P.O. Box 422, E-commerce park<br>Vredenberg<br><br><br>Curaçao"
str1 = "Adelgatan 21<br>Malmö<br><br>211 22<br>Sweden"
str2 = "La¨ntinen Pitka¨katu 35 A 15<br>Turku<br><br>20100<br>Finland"
Countries = Right(str, Len(str) - InStrRev(str, "<br>") - 3)
Countries = Countries + vbNewLine + Right(str1, Len(str1) - InStrRev(str1, "<br>") - 3)
Countries = Countries + vbNewLine + Right(str2, Len(str2) - InStrRev(str2, "<br>") - 3)
MsgBox Countries
End Sub
Obviously this will need to be updated for how your data set is stored. You can loop through the dataset and use the string modifier on each line
A formula works too. If a string in A1, write in B1:
=TRIM(RIGHT(SUBSTITUTE(A1,"<br>",REPT(" ",100)),100))
Modified using an approach taken from here:
https://exceljet.net/formula/get-last-word

Parsing name/value pairs containing spaces delimited by spaces

I'm trying to parse a file in vb.net. This file is the output of a CLI command on Mikrotik RouterOS.
The file looks like this, where the \ and the end of the line means the line continues below
# jan/03/2017 12:46:35 by RouterOS 6.38
# software id = 3BQ2-2I1I
#
/queue simple
add dst=WAN max-limit=5M/20M name=Compartido01 priority=1/1 queue=\
wireless-default/wireless-default target="" total-priority=1
add max-limit=512k/2M name="6206101450 - Simone Walter (2M) (P13)" parent=\
Compartido01 target=190.211.88.1/32
add max-limit=350k/1M name=\
"6206130537 - Figueroa Viviana del Carmen (1M) (P14)" parent=Compartido01 \
target=190.211.88.24/32
I managed to skip the 4 first lines and collapse them so they look like this
"add dst=WAN max-limit=5M/20M name=Compartido01 priority=1/1 queue=wireless-default/wireless-default target="" total-priority=1"
"add max-limit=512k/2M name="6206101450 - Simone Walter (2M) (P13)" parent=Compartido01 target=190.211.88.1/32"
"add max-limit=350k/1M name="6206130537 - Figueroa Viviana del Carmen (1M) (P14)" parent=Compartido01 target=190.211.88.24/32"
What I need to do is to extract the information on those strings, something like "name=XXXXXX" and "target=XXXXX"
I could split using space as delimiter but the "name" field can have spaces inside it
Anyone can give me a hint ?
What you need is a RegEx match parser...Found one here. See if you can make what you need from this.
Imports System.Text.RegularExpressions
Module Parser
Public Function ParseKeyValuePairs(ByVal Buffer As String) As Dictionary(Of String, String)
Dim Result = New Dictionary(Of String, String)
'---- There are 3 sub patterns contained here, seperated at the | characters
' The first retrieves name="value", honoring doubled inner quotes
' The second retrieves name=value where value can't contain spaces
' The third retrieves name alone, where there is no "=value" part (ie a "flag" key
' where simply its existance has meaning
Dim Pattern = "(?:(?<key>[\w-]+)\s*\=\s*""(?<value>[^""]*(?:""""[^""]*)*)"") | " & _
"(?:(?<key>[\w-]+)\s*\=\s*(?<value>[^""\s]*)) | " & _
"(?:(?<key>[\w-]+)\s*)"
Dim r = New System.Text.RegularExpressions.Regex(Pattern, RegexOptions.IgnorePatternWhitespace)
'---- parse the matches
Dim m As System.Text.RegularExpressions.MatchCollection = r.Matches(Buffer)
'---- break the matches up into Key value pairs in the return dictionary
For Each Match As System.Text.RegularExpressions.Match In m
Result.Add(Match.Groups("key").Value, Match.Groups("value").Value)
Next
Return Result
End Function
Public Sub Main()
Dim s = "Key1=Value Key2=""My Value here"" Key3=Test Key4 Key5"
Dim r = ParseKeyValuePairs(s)
For Each i In r
Debug.Print(i.Key & "=" & i.Value)
Next
End Sub
End Module

How can I pick specific string fragments out of an excel cell using a custom formula written in VBA

At work I am required to reformat incorrect Addresses on a weekly basis from records in our Salesforce instance. We gather the incorrectly formatted addresses using a Report and export them to an Excel file. My job is simply to manipulate the data in the file to format them properly then reinsert them into the database.
Typically the addresses are formatted as so:
5 Sesame Street, Anytown, Anyplace
Separating these can be done easily by hand, but I typically have to work with hundreds of addresses at a time, and using default excel formulas tends to require lots of wrangling multiple cells at once to break it up into fragments.
Thus I wrote a custom formula to run through the cell and return a specific fragment of the string based on the "Comma Number" given. So if I give a Comma Number of 1, I would get "5 Sesame Street", 2 would get me "Anytown", etc.
Here is my code so far:
Public Function fragmentAddress(address As String, numberofcommas As Integer) As String
seen = 1
lastComma = -1
Dim x As Long
Dim frag As Long
For x = 0 To Len(address)
If Mid(address, x, 1) = "," & numberofcommas = seen Then
Exit For
ElseIf Mid(address, x, 1) = "," & numberofcommas <> seen Then
seen = seen + 1
lastComma = x
End If
Next
frag = Mid(address, lastComma + 1, seen - lastComma)
fragmentAddress = frag
I have not implemented the ability to handle the final value yet, but it does not give me any outputs, only outputting a "#VALUE!" error when I attempt to give it the input
=fragmentAddress("3 Ashley Close, Charlton Kings",1)
I have some experience with programming, but this is my first time writing anything in VBA.
Any help would be appreciated, thank you.
Not exactly sure what your question is, but this is simpler:
Public Function GetAddressFragment(ByVal Address As String, ByVal Index As Integer) As String
Dim addr() As String
addr = Split(Address, ",")
On Error Resume Next
GetAddressFragment = Trim(addr(Index - 1))
End Function

Resources