HeSanbi
Nam Nhân✨Hiếu Lễ Nghĩa Trí Tín✨
- Tham gia
- 24/2/13
- Bài viết
- 2,774
- Được thích
- 4,401
- Giới tính
- Nam
Bạn có thể thử hàm dưới đây:Tại mình mới dùng VBA. Bạn có thể ghi giúp mình không ạ.
Mình mới chỉ tách được tên. Nhưng những tên trùng thì nó không hiện ra được số(
Hàm có sử dụng từ khóa để định hình khối dữ liệu phân tích, để giúp bạn sử dụng được nhiều nơi nhiều lần
Hàm có tham số format để dễ dàng định dạng tên trùng
=ShortName("A B C")
JavaScript:
Function ShortName(ByVal FullName As String, Optional format As String = "0#", Optional key As String = "C") As String
On Error Resume Next
Dim i As Byte, v$, r As Range, D0
Static D As Object
If key = "" Then
Set r = Application.Caller
If TypeName(r) = "Range" Then
key = r.EntireColumn.Address(0, 0, external:=1)
Else
key = ActiveCell.EntireColumn.Address(0, 0, external:=1)
End If
End If
If Not D Is Nothing Then
If IsNumeric(FullName) Then Set D = Nothing
End If
If D Is Nothing Then
Set D = VBA.CreateObject("Scripting.Dictionary"): D.CompareMode = 1
End If
If Not D.exists(key) Then
Set D0 = VBA.CreateObject("Scripting.Dictionary"): D0.CompareMode = 1
D.Add key, D0
Else
Set D0 = D(key)
End If
FullName = Trim(FullName): v = Left(FullName, 1)
For i = 1 To Len(FullName) - 1
If Mid(FullName, i, 2) Like " [! ]" Then v = v + Mid(FullName, i + 1, 1)
Next
If D0.exists(v) Then
i = D0(v): D0.Remove v: D0.Add v, i + 1
v = v & VBA.format(i, format)
Else
D0.Add v, 0
End If
ShortName = v
End Function