我有一个包含以下数据的多行单元格,我需要一个找到最高年龄并给出结果的函数。您可以在括号中找到年龄。如果多个人的年龄相同,年龄最大,则应将两者作为单元格中的输出
实施例:1
Ravi (11)\,Chandra (22)\,Krishna (22)\,Hemanth (11)
对于上述日期,它应该在单元格中给出如下输出:
Chandra (22)\,Krishna (22)
实施例:2
Ravi (11)\,Chandra (23)\,Krishna (22)\,Hemanth (11)
在单元格中输出如下:
Chandra (23)
答案 0 :(得分:0)
你可以尝试这个功能:
Function AgeResult(cellVal As String) As String
Dim elem As Variant
Dim maxAgeNames As String
Dim iniPos As Long, endPos As Long
Dim maxAge As Long, age As Long
For Each elem In Split(cellVal, ",")
iniPos = InStr(elem, "(")
endPos = InStr(elem, ")")
age = Mid(elem, iniPos + 1, endPos - iniPos - 1)
If age >= maxAge Then
If age > maxAge Then
maxAge = age
maxAgeNames = elem
Else
maxAgeNames = maxAgeNames & "," & elem
End If
End If
Next elem
AgeResult = maxAgeNames
End Function
可能的用法如下:
Sub main()
Dim cell As Range
For Each cell In Range("A1:A10") '<~~ range of cells to process the content of
cell.Offset(, 1) = AgeResult(cell.Value) '<~~ this puts the resulting string in the adjacent cell to the right
Next cell
End Sub