VBA to create list from range based on unique first five characters of the string

sureng19

New Member
Joined
Jan 4, 2013
Messages
11
Office Version
  1. 2021
Platform
  1. Windows
Some data strings in column A has common first five characters. I want to extract those first five characters and create a list in column B where there will not be any duplicates.
Please see the attached image. I was trying to manipulate code that used a "Scripting Dictionary" and trim the string to left 5 characters but was not going anywhere. Thanks in advance.
project.jpg
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Some data strings in column A has common first five characters. I want to extract those first five characters and create a list in column B where there will not be any duplicates.
Please see the attached image. I was trying to manipulate code that used a "Scripting Dictionary" and trim the string to left 5 characters but was not going anywhere. Thanks in advance. View attachment 106567
This will return your unique first five characters, but will be a spilled result:
VBA Testing.xlsm
AB
1ListUnique
221117 Mangos 00121117
321117 Mangos 00221045
421117 Mangos 00321046
521117 Bananas 00121047
621117 Bananas 00242987
721045 Apples 00142980
821046 Apples 001
921047 Green Apples 001
1021047 Red Apples 001
1142987 Mangos 001
1242980 Mangos 001
UniqueRight
Cell Formulas
RangeFormula
B2:B7B2=UNIQUE(LEFT(A2:A12,5))
Dynamic array formulas.
 
Upvote 0
VBA option ...
VBA Code:
Sub test()

With CreateObject("scripting.dictionary")
    For x = 2 To Range("A" & Rows.Count).End(3).Row
        If Not .exists(Left(Cells(x, 1), 5)) Then .Add Left(Cells(x, 1), 5), Nothing
    Next
    Cells(2, 2).Resize(.Count) = Application.Transpose(.keys)
End With

End Sub
 
Upvote 0
Solution
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
VBA option ...
VBA Code:
Sub test()

With CreateObject("scripting.dictionary")
    For x = 2 To Range("A" & Rows.Count).End(3).Row
        If Not .exists(Left(Cells(x, 1), 5)) Then .Add Left(Cells(x, 1), 5), Nothing
    Next
    Cells(2, 2).Resize(.Count) = Application.Transpose(.keys)
End With

End Sub
Peter, Thank you very much. Works exactly the way I want. Script turned out to be lot simpler than I thought and simple enough for me to learn a tad more about VBA. Thanks again!!
 
Last edited by a moderator:
Upvote 0
Works exactly the way I want. Script turned out to be lot simpler than I thought and simple enough for me to learn a tad more about VBA. Thanks again!!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.

Peter, Thank you very much.
Secondly, I did not provide the answer so I think that you are thanking the wrong person.

Thirdly, what I did ask you was to put your Excel version in your account details so helpers can provide the best help to you in future & you haven't done that yet. ;)
 
Upvote 0
VBA option ...
VBA Code:
Sub test()

With CreateObject("scripting.dictionary")
    For x = 2 To Range("A" & Rows.Count).End(3).Row
        If Not .exists(Left(Cells(x, 1), 5)) Then .Add Left(Cells(x, 1), 5), Nothing
    Next
    Cells(2, 2).Resize(.Count) = Application.Transpose(.keys)
End With

End Sub
mse330, Thanks for the solution. By mistake I thanked the wrong person and only today I realized that. Nevertheless, thank you.
 
Upvote 0
.. what I did ask you was to put your Excel version in your account details ..
Thanks for doing that. (y)

BTW, you could use the formula concept suggested by @zero269 to produce a vba solution that doesn't require looping through each cell in the range.

VBA Code:
Sub test2()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Cells(1, 2).Formula2 = "=unique(left(" & .Address & ",5))"
    .Cells(1, 2).SpillingToRange.Value = .Cells(1, 2).SpillingToRange.Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,942
Messages
6,181,905
Members
453,070
Latest member
sivasj

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