VBA Split String into 2 groups - vba

my string may have short or long name like below
short: String = "Stack Over Flow"
long: String = "Stack Over Flow Access VBA Coding"
now I need to split this string into 2 groups as
1st group will have first 3 words Stack Over Flow and 2nd group will have Access VBA Coding
if the string is short then 2nd group will have blank
below code does not work need your help
Dim str As String, Result As String
Dim Start_Point As Long, No_Characters As Long
str = cmbName.Text
Start_Point = InStr(str, " ") + 1
No_Characters = Len(str) - Start_Point
group1 = Left(str, No_Characters + 1)
group2 = Right(str, No_Characters + 1)
MsgBox group1 & " - " & group2

Try using Limit parameter of Split function like this
Private Function SplitInTwo(sText As String) As Variant
Dim vSplit As Variant
Dim sSecond As String
vSplit = Split(sText, " ", Limit:=4)
If UBound(vSplit) >= 3 Then
sSecond = vSplit(3)
ReDim Preserve vSplit(0 To 2) As String
End If
SplitInTwo = Array(Join(vSplit, " "), sSecond)
End Function
Here are some use-cases
Dim vParts As Variant
vParts = SplitInTwo("Stack Over Flow")
Debug.Print vParts(0) & " - " & vParts(1) '--> Stack Over Flow -
vParts = SplitInTwo("Stack Over Flow Access VBA Coding")
Debug.Print vParts(0) & " - " & vParts(1) '--> Stack Over Flow - Access VBA Coding

Related

Segregate Alphanumeric Characters from a String of Mixed Characters Inside an Excel Cell using VBA code

Function xxx(str As String) As String
Dim strarr() As String
str = Replace(str, ".", " ")
strarr = Split(str)
Dim i As Long
For i = 0 To UBound(strarr)
If strarr(i) Like "[A-Za-z0-9]" Then
numfromstring = numfromstring & "," & strarr(i)
End If
Next i
numfromstring = Mid(numfromstring, 2)
End Function
Hi Guys, I was trying to segregate only numeric characters from a string of mixed characters inside a cells using VBA with above code and i got the results also.. but now i need the same thing to work for alphanumeric characters. Please advise what change will bring that result. :-)
Get Alpha-Numeric UDF
Function GetAlphaNumeric( _
ByVal Str As String, _
Optional ByVal Delimiter As String = ",") _
As String
Dim n As Long, Char As String, rStr As String
For n = 1 To Len(Str)
Char = Mid(Str, n, 1)
If Char Like "[A-Za-z0-9]" Then rStr = rStr & Delimiter & Char
Next n
If Len(rStr) > 0 Then GetAlphaNumeric = Mid(rStr, 1 + Len(Delimiter))
End Function

Rename PDF files with VBA

I am trying to rename some pdf files with this kind of name: "2020-01-24-GOOGLE.NY-JPM-XXXXXXXXX.pdf"
into: "2020 01 24 - GOOGLE - JPM - 30p.pdf" with 30p meaning 30 pages (the number of pages in the pdf file).
The structure of the name is always the same, only the letters / numbers change.
I have already prepared some code (that you can find below), yet I am struggling with two things:
How can I "extract" the Broker name, (here JPM)
How can I get the number of pages in the pdf ? I have seen some solutions on the forum requiring Adobe Pro, yet I do not have access to it
Do you have any ideas to solve this problem ?
Here is the code:
Sub FetchName()
Dim nameArray() As Variant
Dim renameArray() As Variant
Dim myPath As String
Dim myFile As String
Dim r As Integer
Dim Year As String
Dim Month As String
Dim Day As String
Dim Company As String
Dim Broker As String
Dim NPage As String
Dim numElements As Integer
Dim s As Integer
Dim t As Integer
Dim AcroDoc As Object
Dim StartNum As Integer
Dim numCar As Integer
'get two inputs
myPath = Worksheets("Cover").Cells(3, 4)
Company = Worksheets("Cover").Cells(3, 2)
'get names in an array
myFile = Dir(myPath & "*.pdf")
r = 1
Do While myFile <> ""
ReDim Preserve nameArray(r)
nameArray(UBound(nameArray)) = myFile
r = r + 1
myFile = Dir
Loop
numElements = UBound(nameArray) - LBound(nameArray) + 1
'prepare array with new names
s = 1
For s = 1 To numElements
Year = Left(nameArray(s), 4)
Month = Mid(nameArray(s), 6, 2)
Day = Mid(nameArray(s), 9, 2)
StartNum = InStr(1, Replace(nameArray(s), "-", "~", 4), "~")
numCar = InStr(1, Replace(nameArray(s), "-", "~", 5), "~") - InStr(1, Replace(nameArray(s), "-", "~", 4), "~") + 1
Broker = Mid(nameArray(s), StartNum, numCar)
'numpage
'ReDim Preserve renameArray(r)
'renameArray(UBound(renameArray)+1) = Year & " " & Month & " " & Day & " - " & Company & " - " & Broker & " - " & NPage & "p"
s = s + 1
Next s
'rename files with renameArray
t = 1
For t = 1 To numElements
Name myPath & nameArray(1) As myPath & renameArray(1)
t = t + 1
Next t
End Sub
enter code here
For the Broker name, you can use InStrRev to search for the position of the last and second last dashes:
namePDF = "2020-01-24-GOOGLE.NY-JPM-XXXXXXXXX.pdf"
lastDashAt = InStrRev(namePDF, "-")
secondLastDashAt = InStrRev(namePDF, "-", lastDashAt - 1)
Broker = Mid(namePDF, secondLastDashAt + 1, lastDashAt - secondLastDashAt - 1)

VBA, 2nd last "/" using InstrRev

I have code that parses out the last word on a string.
ie. Stack/Over/Flow will give me "Flow".
But I want to get "Over/Flow".
This is what I got, but only able to get "Flow"
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/") + 1) & "'"
I would use Split()
Sub lastTwo()
Dim str As String
str = "Stack/Over/Flow"
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) > 0 Then
Debug.Print splt(UBound(splt) - 1) & "/" & splt(UBound(splt))
End If
End Sub
Here is a function that does it:
Function lastParts(str As String, delim As String, x As Long) As String
Dim splt() As String
splt = Split(str, "/")
If UBound(splt) + 1 >= x Then
Dim t As String
t = "=INDEX(INDEX({""" & Join(splt, """;""") & """},N(IF({1},ROW(" & UBound(splt) - x + 2 & ":" & UBound(splt) + 1 & "))),),)"
lastParts = Join(Application.Transpose(Application.Evaluate(t)), delim)
Else
lastParts = str
End If
End Function
It has three parts, the string, the delimiter and the number of returns.
It can be called using your code:
arr(counter-2) = lastParts(Text,"/",2)
or from the worksheet
=lastParts(A1,"/",2)
Initially misread the question. You can nest InStrRev() calls
arr(counter - 2) = "'" & mid(Text, InStrRev(Text, "/",InStrRev(Text, "/")-1)+1) & "'"

How to Display returned mystring value as date in VBA

I have created a function to get absent days based on absence entries in my attendance sheet.
My code is:
Dim cell As Range
Dim myString As String
'Loop through all the cells in the range
For Each cell In myRange
'Check for value
If cell = "A" Then
myString = myString & Cells(myRow, cell.Column) & ", "
End If
Next cell
'Return string
If Len(myString) > 0 Then
AbsentDays = Left(myString, Len(myString) - 2)
End If
End Function
I want to display the returned values displayed as "dd mmm".
I am assuming that AbsentDays is a String, and you are getting 6/7/2017, 6/9/2017, 6/15/2017, 6/16/2017 as a String (and not a Date).
Try the String conversion code below:
Dim AbsentDays As String
Dim DatesArr As Variant
Dim i As Long
' for my tests
AbsentDays = "6/7/2017, 6/9/2017, 6/15/2017, 6/16/2017"
' use the split to get the values in an array
DatesArr = Split(AbsentDays, ",")
Dim myDateformat As Variant
' loop through array
For i = 0 To UBound(DatesArr)
If i = 0 Then ' first array element
myDateformat = Format(CDate(DatesArr(i)), "dd mmm")
Else ' 2nd array element and above
myDateformat = myDateformat & ", " & Format(CDate(DatesArr(i)), "dd mmm")
End If
Next i
MsgBox myDateformat
Just because I like to keep changes to a minimum, I think you can simply change your line saying:
myString = myString & Cells(myRow, cell.Column) & ", "
to be:
myString = myString & Format(CDate(Cells(myRow, cell.Column)), "dd mmm") & ", "

String to abbreviation

I'm a graphic artist, new to Excel and VBA but trying to use it to process mountains of data in excel to be used as variable data in Illustrator.
If I want to convert cells with product names for signs like "Budwieser, Bud Light & Bud Black Crown" to an abbreviation following the format "Budweiser_BL_BBC"
I have written a function that I thought would accomplish my task but it returns #VALUE!
Edit
To explain the logic: my idea was to take the string, split it on " & " and then split the first position of the resulting array on ", " then adding what was after the "&" to the end of the second array - this array, sProd, has the products separated into different positions of the array.
Then looping through that array and splitting each product at the spaces creating a jagged array.
Then loop through that array again creating a string taking only the first letter of each word in each product, separating products with an underscore. The exception being that the first word of the first product is spelled out and set in proper case. (Just saw an error in my logic and added the code for the first word exception).
Edit #2
The function should return a string with the first word of the original string set in proper case with all other words abbreviated to their first letter and products separated by underscores. So "Budweiser, Bud Light & Bud Light Lime" returns "Budweiser_BL_BLL", "All Coke & Dr Pepper Products" would return "AllC_DPP" and "Gatorade" returns "Gatorade".
This is my first bout with Excel and VBA.
Function Abbrev(p As String) As String
Dim sAmpersand() As Variant
Dim sProd() As Variant
sAmpersand = Split(p, " & ")
sProd = Split(sAmpersand(0), ", ")
sProd(UBound(sProd)) = sAmpersand(1)
Dim ProductCount As Integer
Dim ProductEnd As Integer
ProductEnd = UBound(sProd) - 1
For ProductCount = 0 To ProductEnd
sProd(ProductCount) = Split(sProd(ProductCount), " ")
ProductCount = ProductCount + 1
Next ProductCount
Dim WordCount As Integer
Dim WordEnd As Integer
WordEnd = UBound(sProd(ProductCount)) - 1
Abbrev = StrConv(sProd(0)(0), vbProperCase)
For ProductCount = 0 To ProductEnd
For WordCount = 0 To WordEnd
If ProductCount = 0 Then
WordCount = 1
End If
Abbrev = Abbrev & Left(sProd(ProductCount)(WordCount), 1)
WordCount = WordCount + 1
Next WordCount
If ProductCount + 1 < ProductEnd Then
Abbrev = Abbrev & "_"
End If
ProductCount = ProductCount + 1
Next ProductCount
End Function
Working code:
Function Abbrev(p As String) As String
Dim res As String, w1, w2
res = Split(Split(p, ",")(0), " ")(0)
If res = Split(p, ",")(0) Then res = res & "_"
For Each w1 In Split(Mid(Replace(p, " &", ","), Len(res) + 1), ",")
For Each w2 In Split(w1, " ")
res = res & Left(w2, 1)
Next w2
res = res & "_"
Next w1
Abbrev = IIf(Right(res, 1) <> "_", res, Left(res, Len(res) - 1))
End Function
Here's a better abbreviate function:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String
I = InStr(Name, " ")
If I < 1 Then
Abbreviate = Name
Exit Function
End If
sResult = Left$(Name, I)
sTemp = Name
Do While I > 0
sTemp = Right$(sTemp, Len(sTemp) - I)
If Left$(sTemp, 1) = "(" Then
If Mid$(sTemp & "***", 3, 1) = ")" Then
sResult = sResult & " " & Left$(sTemp, 3)
Else
sResult = sResult & " " & Left$(sTemp, 1)
End If
Else
sResult = sResult & " " & Left(sTemp, 1)
End If
I = InStr(sTemp, " ")
Loop
Abbreviate = sResult
End Function
This is from user al_b_cnu on mrexcel.com
Here is a modified version to shorten up the result a bit:
Function Abbreviate(Name As String) As String
Dim I As Integer
Dim sResult As String
Dim sTemp As String
I = InStr(Name, " ")
If I < 1 Then
Abbreviate = Name
Exit Function
End If
sResult = Left$(Name, I)
sTemp = Name
Do While I > 0
sTemp = Right$(sTemp, Len(sTemp) - I)
If Left$(sTemp, 1) = "(" Then
If Mid$(sTemp & "***", 3, 1) = ")" Then
sResult = sResult & Left$(sTemp, 3)
Else
sResult = sResult & Left$(sTemp, 1)
End If
Else
sResult = sResult & Left(sTemp, 1)
End If
I = InStr(sTemp, " ")
Loop
Abbreviate = sResult
End Function