Extracting first letter

RIZVI

Active Member
Joined
Jan 1, 2011
Messages
295
Office Version
  1. 2010
Platform
  1. Windows
Hi All,

I am using the following VBA to extract first letters from SKU list. All is fine except when couple of SKUs have matching starting letters, then I get duplicate results. Like say there are items like Chicken Crispy, Chicken Chilly then in both cases i get CC.

Is there a way that ONLY in such conflicing cases ,I get different results say like CCr and CCh instead of CC in both cases.

VBA that I m using is geiven below:

Function GetFirstLetters(rng As Range) As String
'Update 20140325
Dim arr
Dim I As Long
arr = VBA.Split(rng, " ")
If IsArray(arr) Then
For I = LBound(arr) To UBound(arr)
GetFirstLetters = GetFirstLetters & Left(arr(I), 1)
Next I
Else
GetFirstLetters = Left(arr, 1)
End If
End Function
 
OK, the method I came up with simply add a counter to the end of the value if a duplicate (i.e. 1, 2, 3, ...).

This works in conjunction with your current function. Here is the new VBA procedure:
VBA Code:
Sub PopulateItemIDs()

    Dim lr As Long
    Dim rngB As Range
    Dim rngA As Range
    Dim cell As Range
    Dim itemIDBase As String
    Dim itemID As String
    Dim i As Long
    Dim valid As Boolean
    
    Application.ScreenUpdating = False
    
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Build ranges
    Set rngA = Range("A3:A" & lr)
    Set rngB = Range("B3:B" & lr)
    
'   Loop through all cells
    For Each cell In rngB
'       Make sure cell is not empty
        If cell <> "" Then
'           Calculate Item Id
            itemIDBase = GetFirstLetters(cell)
'           Initialize values
            i = 0
            valid = False
            itemID = itemIDBase
'           Loop through until a unique value is created
            Do Until valid = True
'               See if value is unique on sheet
                If Application.WorksheetFunction.CountIf(rngA, itemID) = 0 Then
                    valid = True
                Else
'                   Add to counter
                    i = i + 1
'                   Add to end of itemID
                    itemID = itemIDBase & i
                End If
            Loop
'           Populate column A
            cell.Offset(0, -1).Value = itemID
        End If
    Next cell
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
    
End Sub

And here is the simple example I made up.
The old results with using the function straight on the sheet are shown in column C (results shown in red).
The new results are put in column A by running the VBA procedure above (results shown in blue).
1722973860404.png


You can see that lines 8, 9, and 11 would have shown duplicates before.
 
Upvote 1
Solution

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
OK, the method I came up with simply add a counter to the end of the value if a duplicate (i.e. 1, 2, 3, ...).

This works in conjunction with your current function. Here is the new VBA procedure:
VBA Code:
Sub PopulateItemIDs()

    Dim lr As Long
    Dim rngB As Range
    Dim rngA As Range
    Dim cell As Range
    Dim itemIDBase As String
    Dim itemID As String
    Dim i As Long
    Dim valid As Boolean
  
    Application.ScreenUpdating = False
  
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
  
'   Build ranges
    Set rngA = Range("A3:A" & lr)
    Set rngB = Range("B3:B" & lr)
  
'   Loop through all cells
    For Each cell In rngB
'       Make sure cell is not empty
        If cell <> "" Then
'           Calculate Item Id
            itemIDBase = GetFirstLetters(cell)
'           Initialize values
            i = 0
            valid = False
            itemID = itemIDBase
'           Loop through until a unique value is created
            Do Until valid = True
'               See if value is unique on sheet
                If Application.WorksheetFunction.CountIf(rngA, itemID) = 0 Then
                    valid = True
                Else
'                   Add to counter
                    i = i + 1
'                   Add to end of itemID
                    itemID = itemIDBase & i
                End If
            Loop
'           Populate column A
            cell.Offset(0, -1).Value = itemID
        End If
    Next cell
  
    Application.ScreenUpdating = True
  
    MsgBox "Macro complete!"
  
End Sub

And here is the simple example I made up.
The old results with using the function straight on the sheet are shown in column C (results shown in red).
The new results are put in column A by running the VBA procedure above (results shown in blue).
View attachment 115052

You can see that lines 8, 9, and 11 would have shown duplicates before.
If I am to add 1,2,3 at the end of the items, then my VBA will also resolve the issue of duplicates. This simple idea of adding 1.2.3 at the end of items did not occur to me. and I thank you for that as it solves my issue and in a much simpler way.

RGDS,

Rizvi.M.H.
 
Upvote 0
You are welcome! Glad I was able to help.
Sometimes it helps to have a fresh set of eyes to look at.

I have done stuff like this before, and tried things where you then take the second letter, but what if that then is also a duplicate? And even if you go to the next one, that could still cause a duplicate.
And it could go on-and-on and you have no idea how far you need to program out to. That is when I decided that a counter works much better, and then you do not need to worry about the other stuff!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top