Sub TaoMaPhieu()
Dim lastRow As Long, i As Long, j As Long
Dim ngay As Date
Dim namMa As String, thangMa As String, ngayMa As String, loaiMa As String
Dim soThuTu As String
Dim countSame As Integer
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
If IsDate(Cells(i, 1).Value) Then
ngay = Cells(i, 1).Value
' Mã nam '
Select Case Year(ngay)
Case 2025: namMa = "E"
Case 2026: namMa = "F"
Case Else: namMa = "?"
End Select
' Mã tháng '
Select Case Month(ngay)
Case 10: thangMa = "A"
Case 11: thangMa = "B"
Case Else: thangMa = CStr(Month(ngay))
End Select
' Mã ngày '
Select Case Day(ngay)
Case 19: ngayMa = "J"
Case Else: ngayMa = CStr(Day(ngay))
End Select
' Mã lo?i phi?u '
Select Case Cells(i, 2).Value
Case "PC_": loaiMa = "C"
Case "PT_": loaiMa = "T"
Case "PCK": loaiMa = "CK"
Case Else: loaiMa = "?"
End Select
' Ð?m s? dòng tru?c dócó cùng ngày và lo?i phi?u '
countSame = 0
For j = 2 To i
If Cells(j, 1).Value = Cells(i, 1).Value And Cells(j, 2).Value = Cells(i, 2).Value Then
countSame = countSame + 1
End If
Next j
' S? th? t? d?ng 3 ch? s? '
soThuTu = Format(countSame, "000"
' Ghi mã phi?u vào c?t C '
Cells(i, 3).Value = namMa & thangMa & ngayMa & loaiMa & "_" & soThuTu
End If
Next i
MsgBox "Ðã t?o mã phi?u xong!", vbInformation
End Sub