Home » excel » excel – String to abbreviation

excel – String to abbreviation

Posted by: admin May 14, 2020 Leave a comment

Questions:

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
How to&Answers:

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

enter image description here

Answer:

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