Build alphabetical order if the word is in different part of the sentence

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Hi!

This seems a very interesting code for me, however I couldn't figure out how I could solve it.

I need to build an alphabeticel order from the words of cells.
Sample:

Column "A" (Input)

The apple is red.
Banana is unhealthy.
Cambridge is a very nice place.
Buy a camion.
Visiting Canada is always a fun.

Column "B" (Output)
apple
banana
cambridge
camion
canada (upper and lower case doesnt matter)

So if B1 is given (apple) and we compare each word of a cell in column "A" to find the closest word to apple, then the list could be built.

The apple is red. Apple
Banana is unhealthy.
From these words which is the nearest to apple? --> Banana
Cambridge is a very nice place.
to Banana? --> Cambridge
Buy a camion.
and so on...
Visiting Canada is always a fun.

Or if we make an alphabetical order using the words in A2 (Banana is unhealthy) and B1 (Apple), then the word after "apple" is the word we need
1. apple
2. banana
3. is
4. unhealthy

Banana is after apple, so that is what we need.

Sory for the long explanation.

Is there anybody, who could solve this using VBA code?

Thank you for your help! Have a very nice day! :-)
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I need to build an alphabeticel order from the words of cells.
Sample:

Column "A" (Input)

The apple is red.
Banana is unhealthy.
Cambridge is a very nice place.
Buy a camion.
Visiting Canada is always a fun.

Column "B" (Output)
apple
banana
cambridge
camion
canada (upper and lower case doesnt matter)
There might be some further issues with punctuation etc, but see if this is headed in the right direction.


Run the 'MakeList' macro
Code:
Sub MakeList()
  Dim CurrWord As String
  Dim c As Range

  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
    CurrWord = WordAfter(c.Value, CurrWord)
    c.Offset(, 1).Value = CurrWord
  Next c
End Sub

Function WordAfter(s As String, sWord As String) As String
  Static AL As Object
  Dim SortedWords As Variant, e As Variant
  
  If AL Is Nothing Then Set AL = CreateObject("System.Collections.ArrayList")
  AL.Clear
  For Each e In Split(s)
    If Not AL.Contains(e) Then AL.Add e
  Next e
  AL.Add sWord
  AL.Sort
  SortedWords = AL.ToArray
  WordAfter = SortedWords(Application.Match(sWord, SortedWords, 0))
End Function

Data in col A, result of code in col B.


Book1
AB
1The apple is red.apple
2Banana is unhealthy.Banana
3Cambridge is a very nice place.Cambridge
4Buy a camion.camion.
5Visiting Canada is always a fun.Canada
Alphabetical
 
Upvote 0
Hi Peter!

Thank you for your quick reply, I'm very excited to see you could solve this so fast!

I see, that it works fine for you, but by me it highlights the last row:
WordAfter = SortedWords(Application.Match(sWord, SortedWords, 0))

I run the code on the sample too, (giving the value "apple" to the cell of B1) but unfortunately something is wrong on my side.
https://imgur.com/a/YPmqxgj

Do you have any idea? (Excel 2016)
 
Last edited:
Upvote 0
Yes, my mistake, I had an error in the data that led me to use incorrect code.
Still could be problems, but try changing the function part to this
Code:
Function WordAfter(s As String, sWord As String) As String
  Static AL As Object
  Dim SortedWords As Variant, e As Variant
  
  If AL Is Nothing Then Set AL = CreateObject("System.Collections.ArrayList")
  AL.Clear
  For Each e In Split(s)
    If Not AL.Contains(e) Then AL.Add e
  Next e
  If sWord <> "" Then AL.Add sWord
  AL.Sort
  SortedWords = AL.ToArray
  WordAfter = SortedWords(IIf(sWord = "", 0, Application.Match(sWord, SortedWords, 0)))
End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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