Function SaiRangChiu(s As String) As String
' get rid of repeating and redundant subject names
' That is: TinA1. TinA2. TinA3. LyA1. LyA2. LyA3. HoaA1. HoaA2. -> TinA1.A2.A3. LyA1.A2.A3. HoaA1.A2
' within the following code: Toan, Tin,... are called subject categories, and A1, A2 are subject subdivisions
Dim aSubjectCats ' serves as an array of subject categories
Dim aSubjects() As String ' this array is for packing subjects into their respective cats
Dim sPart ' represents each subject element within input string
Dim aLo As Integer, aHi As Integer, cat As Integer ' counters
aSubjectCats = Array("Toan", "Tin", "Ly", "Hoa", "Van") ' define subject categories
aLo = LBound(aSubjectCats)
aHi = UBound(aSubjectCats)
ReDim aSubjects(aLo To aHi)
For Each sPart In Split(Replace(s, " ", ""), ".") ' traverse input string in parts
For cat = aLo To aHi
If sPart Like aSubjectCats(cat) & "*" Then Exit For ' search subject's category
Next cat
If cat <= aHi Then ' category found
If aSubjects(cat) <> "" Then ' category already registered
aSubjects(cat) = aSubjects(cat) & "." & Replace(sPart, aSubjectCats(cat), "")
Else
aSubjects(cat) = sPart ' place subject in, and at the same time, register cat
End If
End If
Next sPart
SaiRangChiu = Replace(Application.Trim(Join(aSubjects, " ")), " ", ". ")
End Function