Формирует дательный падеж фамилии, имени, отчества.
=DativeCase(A1), =DativeCase(A1;B1;C1), =DativeCase(A1;B1), где:
• sSurname - фамилия;
• sName - имя;
• sPatronymic - отчество.
Option Compare Text 'сравнение без учета регистра
Function DativeCase(sSurname$, Optional sName$, Optional sPatronymic$) As String
Application.Volatile True 'автопересчет формулы на листе
sSurname$ = Replace(sSurname$, " - ", "-"): sSurname$ = Replace(Replace(sSurname$, " -", "-"), "- ", "-")
On Error Resume Next
If sName$ = "" And sPatronymic$ = "" Then
arr = Split(Application.Trim(sSurname$))
sSurname$ = arr(0): sName$ = arr(1): sPatronymic$ = Replace(arr(2), ".", "")
End If
'заканчивается на "вна" или "кызы" - женщины, остальные - мужчины
Dim bMaleSex As Boolean: 'bMaleSex = (Right(sPatronymic, 1) = "ч" Or Right(sPatronymic, 4) = "оглы")
bMaleSex = Not (Right(sPatronymic, 2) = "на" Or Right(sPatronymic, 4) = "кызы")
If Len(sSurname) > 0 Then 'фамилия
arrSurname = Split(sSurname, "-")
For i = LBound(arrSurname) To UBound(arrSurname) 'перебираем все части фамилий, содержащих дефис
sRes = "": sSurnamePart = arrSurname(i)
If bMaleSex Then 'мужские фамилии
Select Case Right(sSurnamePart, 1)
Case "о", "и", "ы", "у", "э", "е", "ю": sRes = sSurnamePart
Case "ь", "й": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 1) & "ю"
Case "я", "а": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 1) & "е"
If UBound(arrSurname) > 0 And i = 0 Then sRes = sSurnamePart
Case Else: sRes = sSurnamePart & "у"
End Select
Select Case Right(sSurnamePart, 2) 'добавлено для редких фамилий
Case "ец": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 2) & "цу"
If LCase(sSurnamePart) Like "*[уеыаоэяиюё]ец" Then sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 1) & "цу"
If LCase(sSurnamePart) Like "*[!уеыаоэяиюё][!уеыаоэяиюё]ец" Then sRes = sSurnamePart & "у"
Case "зе", "их", "ых": sRes = sSurnamePart
Case "ый": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 2) & "ому"
Case "ий", "ой": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 2) & "ому"
If Len(sSurnamePart) <= 4 Then sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 1) & "ю"
If Right(sSurnamePart, 3) = "чий" Then sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 2) & "ему"
Case "уй": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 2) & "ую"
End Select
Else 'женские фамилии
Select Case Right(sSurnamePart, 1)
Case "о", "е", "э", "и", "ы", "у", "ю", "б", "в", "г", "д", "ж", "з", "к", "л", "м", "н", "п", _
"р", "с", "т", "ф", "х", "ц", "ч", "ш", "щ", "ь", "й": sRes = sSurnamePart
Case "я": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 2) & "ой"
Case Else: sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 1) & "ой"
End Select
Select Case Right(sSurnamePart, 2) 'добавлено, для редких фамилий
Case "ха", "ла", "ее": sRes = Mid(sSurnamePart, 1, Len(sSurnamePart) - 1) & "е"
End Select
End If
'не склоняются мужские и женские фамилии, оканчивающиеся на -о, -е, -э, -и, -ы, -у, -ю,
'а также на -а с предшествующей гласной
If LCase(sSurnamePart) Like "*[уеыаоэяиюё]а" Then sRes = sSurnamePart
arrSurname(i) = sRes
Next
DativeCase = Join(arrSurname, "-") & " " 'соединяем части склоняемой фамилии обратно в одну строку
End If
If Len(sName) > 0 Then 'имя
NameException$ = GetDativeException(sName)
If Len(NameException$) Then 'для имен-исключений
DativeCase = DativeCase & NameException$
Else 'имя не найдено в списке исключений
If bMaleSex Then
Select Case Right(sName, 1)
Case "й", "ь": DativeCase = DativeCase & Mid(sName, 1, Len(sName) - 1) & "ю"
Case "я", "а": DativeCase = DativeCase & Mid(sName, 1, Len(sName) - 1) & "е"
Case "о": DativeCase = DativeCase & sName
Case Else: DativeCase = DativeCase & sName & "у"
End Select
Else
Select Case Right(sName, 1)
Case "а", "я"
If Mid(sName, Len(sName) - 1, 1) = "и" Then
DativeCase = DativeCase & Mid(sName, 1, Len(sName) - 1) & "и"
Else
DativeCase = DativeCase & Mid(sName, 1, Len(sName) - 1) & "е"
End If
Case "ь": DativeCase = DativeCase & Mid(sName, 1, Len(sName) - 1) & "и"
Case Else: DativeCase = DativeCase & sName
End Select
End If
End If
DativeCase = DativeCase & " "
End If
If Len(sPatronymic) > 0 Then 'отчество
If Right(sPatronymic, 4) = "оглы" Or Right(sPatronymic, 4) = "кызы" Then
DativeCase = DativeCase & sPatronymic
Else
If bMaleSex Then
DativeCase = DativeCase & sPatronymic & "у"
Else
DativeCase = DativeCase & Mid(sPatronymic, 1, Len(sPatronymic) - 1) & "е"
End If
End If
End If
DativeCase = Replace(DativeCase, "-", "- ")
DativeCase = StrConv(DativeCase, vbProperCase)
DativeCase = Replace(DativeCase, "- ", "-")
End Function
Function GetDativeException(ByVal txt$) As String 'склонение имен-исключений
Select Case txt$
Case "Павел": GetDativeException = "Павлу"
Case "Лев": GetDativeException = "Льву"
Case "Пётр": GetDativeException = "Петру"
Case "Петр": GetDativeException = "Петру"
Case "Любовь": GetDativeException = "Любови"
Case "Ольга": GetDativeException = "Ольге"
'без изменения (не склоняются) - перечисляем через запятую
Case "Али", "Бали": GetDativeException = txt$
End Select
End Function