P
P
Pavel2021-04-05 15:26:35
Visual Basic
Pavel, 2021-04-05 15:26:35

How to get full name from full text in VBA excel?

there is a column with full name (Ivanov Ivan Ivanovich), how to make an abbreviation to Ivanov I.I. ?
Column one, can not be divided

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2021-04-05
@mrusklon

Using regular expressions (you need to connect the library to the project for it to work):
Tools – References…
☑ Microsoft VBScript Regular Expression
Code:

Public Function getShortName(strText As Variant) As String
    Dim myRegExp As New RegExp
    Dim matches As MatchCollection

    myRegExp.Global = True
    myRegExp.IgnoreCase = True
    myRegExp.Pattern = "(\S+)\s+(\S)\S*\s+(\S)\S*"
    Set matches = myRegExp.Execute(strText)
    
    getShortName = matches(0).SubMatches(0) & " " & matches(0).SubMatches(1) & "." & matches(0).SubMatches(2) & "."
End Function

It is possible through Split (), even shorter code will turn out, although it will stumble on double spaces.
Function getShortName(strText As String) As String
    Dim a
    a = Split(strText, " ")    
    getShortName = a(0) & " " & Left(a(1), 1) & "." & Left(a(2), 1) & "."
End Function

And you can generally make a formula - start from the search for text (spaces)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question