Option Explicit
Function MaHoa(ByVal iStr$, ByVal iCode$, ByVal sRow&) As String
  Dim abc$, def$, BangMa$, j&
  abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  def = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  iStr = UCase(iStr)
  BangMa = TronMa(def, UCase(Replace(iCode, " ", "", 1)), sRow) & " "
  abc = abc & " "
  For j = 1 To Len(iStr)
    Mid(iStr, j, 1) = Mid(BangMa, InStr(1, abc, Mid(iStr, j, 1)), 1)
  Next j
  MaHoa = iStr
End Function
Function GiaiMa(ByVal iStr$, ByVal iCode$, ByVal sRow&) As String
  Dim abc$, def$, BangMa$, j&
  abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  def = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  iStr = UCase(iStr)
  BangMa = TronMa(def, UCase(Replace(iCode, " ", "", 1)), sRow) & " "
  abc = abc & " "
  For j = 1 To Len(iStr)
    Mid(iStr, j, 1) = Mid(abc, InStr(1, BangMa, Mid(iStr, j, 1)), 1)
  Next j
  GiaiMa = iStr
End Function
Private Function TronMa(ByVal def$, ByVal iCode$, ByVal sRow&) As String
  Dim Res$, sCol&, j&, jC&, q&, n&
  q = Len(def)
  sCol = (q - 1) \ sRow + 1
  def = iCode & def
  For j = 1 To Len(iCode)
    def = Mid(def, 1, j) & Replace(Mid(def, j + 1, Len(def)), Mid(iCode, j, 1), "")
  Next j
  Res = def
  n = sCol * sRow - 1
  For j = 0 To n
    If j < q Then
      If j Mod sCol = 0 Then jC = j \ sCol + 1 Else jC = jC + sRow
      If jC <= q Then Mid(Res, j + 1, 1) = Mid(def, jC, 1)
    End If
  Next j
  TronMa = Res
End Function