ongke0711 > 20-09-20, 06:33 PM
s = "Sáng:NV1,Sáng:NV2,Sáng:NV3,Chiều:NV1,Chiều:NV2,Tổi:NV5,Tối:NV6"
kq = "Sáng:NV1,NV2,NV3,Chiều:NV1,NV2,Tối:NV5,NV6"
thuyyeu99 > 20-09-20, 06:53 PM
ongke0711 > 20-09-20, 07:00 PM
thuyyeu99 > 20-09-20, 07:04 PM
thuyyeu99 > 20-09-20, 10:38 PM
Function GroupString(TextStrng As String) As String
Dim TmpArr() As String
Dim Result() As String
Dim TmpArr2() As String
Dim DisplayText As String
Dim i, idic, ndic As Long
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
'"Sáng:NV1,NV2,NV3,Chi?u:NV1,NV2,T?i:NV5,NV6"
'"Sáng:NV1,NV2,NV3,Chi?u:NV1,NV2,T?i:NV5,NV6"
TmpArr = Split(TextStrng, ",")
ReDim Result1(1)
For i = LBound(TmpArr()) To UBound(TmpArr())
TmpArr2 = Split(TmpArr(i), ":")
If Not IsEmpty(TmpArr2(0)) And Not dict.exists(TmpArr2(0)) Then
idic = idic + 1
dict.Add TmpArr2(0), idic
ReDim Preserve Result(idic)
Result(idic) = TmpArr2(0) & ":" & TmpArr2(1)
Else
ndic = dict.Item(TmpArr2(0))
Result(ndic) = Result(ndic) & "," & TmpArr2(1)
End If
Next i
DisplayText = Join(Result, ":")
GroupString = Mid(DisplayText, 2, Len(DisplayText))
End Function
ongke0711 > 20-09-20, 10:59 PM
thuyyeu99 > 20-09-20, 11:31 PM
maidinhdan > 21-09-20, 12:36 PM
(20-09-20, 10:38 PM)thuyyeu99 Đã viết: em thử trước nhé
Mã PHP:Function GroupString(TextStrng As String) As String
Dim TmpArr() As String
Dim Result() As String
Dim TmpArr2() As String
Dim DisplayText As String
Dim i, idic, ndic As Long
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
'"Sáng:NV1,NV2,NV3,Chi?u:NV1,NV2,T?i:NV5,NV6"
'"Sáng:NV1,NV2,NV3,Chi?u:NV1,NV2,T?i:NV5,NV6"
TmpArr = Split(TextStrng, ",")
ReDim Result1(1)
For i = LBound(TmpArr()) To UBound(TmpArr())
TmpArr2 = Split(TmpArr(i), ":")
If Not IsEmpty(TmpArr2(0)) And Not dict.exists(TmpArr2(0)) Then
idic = idic + 1
dict.Add TmpArr2(0), idic
ReDim Preserve Result(idic)
Result(idic) = TmpArr2(0) & ":" & TmpArr2(1)
Else
ndic = dict.Item(TmpArr2(0))
Result(ndic) = Result(ndic) & "," & TmpArr2(1)
End If
Next i
DisplayText = Join(Result, ":")
GroupString = Mid(DisplayText, 2, Len(DisplayText))
End Function
Public Function SapXepChuoiSangChieuToi() As Boolean
Dim Mang As Variant, str As String, i As Integer
Dim s As String, c As String, t As String ' Bien Sang,Chieu,Toi
str = ("Sang:NV1,Sang:NV2,Sang:NV3,Chieu:NV1,Chieu:NV2,Toi:NV5,Toi:NV6")
Mang = Split(str, ",") ' phan tach chuoi thanh mang dua tren dau phan cach ','
For i = 0 To UBound(Mang)
If Left((Mang(i)), 1) = "s" Then s = s & Mang(i) & ","
If Left((Mang(i)), 1) = "c" Then c = c & Mang(i) & ","
If Left((Mang(i)), 1) = "t" Then t = t & Mang(i) & ","
Next
str = s & c & t
Debug.Print str
Erase Mang ' giai phong bo nho duoc phan bo cho cac bien mang
End Function
Sang:NV1,Sang:NV2,Sang:NV3,Chieu:NV1,Chieu:NV2,Toi:NV5,Toi:NV6,
ongke0711 > 21-09-20, 01:50 PM
Function GopChuoi() As String
Dim dic As New Dictionary
Dim arrStr() As String, arrStr2() As String, s As String, kq As String
Dim i As Integer, k As Integer
GopChuoi = ""
s = "Sáng:NV1,Sáng:NV2,Sáng:NV3,Chi?u:NV1,Chi?u:NV2,T?i:NV5,T?i:NV6"
arrStr = Split(s, ",")
For i = 0 To UBound(arrStr)
arrStr2 = Split(arrStr(i), ":")
If dic.Exists(Trim(arrStr2(0))) Then
dic.Item(arrStr2(0)) = dic.Item(arrStr2(0)) & "," & arrStr2(1)
Else
dic.Add arrStr2(0), arrStr2(1)
End If
Next
For k = 0 To dic.Count - 1
kq = kq & dic.Keys(k) & ":" & dic.Items(k) & "| "
Next k
GopChuoi = kq
Debug.Print GopChuoi
End Function
Sáng:NV1,NV2,NV3| Chiều:NV1,NV2| Tối:NV5,NV6|
maidinhdan > 21-09-20, 01:55 PM
(21-09-20, 01:50 PM)ongke0711 Đã viết: Cách của tôi: cũng dùng Split + Dictionary, dùng 2 mảng
Tận dụng thuộc tính của Dictionary là loại bỏ key trùng lặp để gộp chuỗi trùng.
Cách của Dân dùng các hàm IF, Left trong từng vòng lặp chưa chắc xử lý nhanh nhé.
Mã PHP:Function GopChuoi() As String
Dim dic As New Dictionary
Dim arrStr() As String, arrStr2() As String, s As String, kq As String
Dim i As Integer, k As Integer
GopChuoi = ""
s = "Sáng:NV1,Sáng:NV2,Sáng:NV3,Chi?u:NV1,Chi?u:NV2,T?i:NV5,T?i:NV6"
arrStr = Split(s, ",")
For i = 0 To UBound(arrStr)
arrStr2 = Split(arrStr(i), ":")
If dic.Exists(Trim(arrStr2(0))) Then
dic.Item(arrStr2(0)) = dic.Item(arrStr2(0)) & "," & arrStr2(1)
Else
dic.Add arrStr2(0), arrStr2(1)
End If
Next
For k = 0 To dic.Count - 1
kq = kq & dic.Keys(k) & ":" & dic.Items(k) & "| "
Next k
GopChuoi = kq
Debug.Print GopChuoi
End Function
Mã PHP:Sáng:NV1,NV2,NV3| Chiều:NV1,NV2| Tối:NV5,NV6|