|
代码修正
- Sub 随机生成编号(ByVal sCodeType As String)
- Dim dicData As Object
- Dim vData As Variant, nRow As Integer, nI As Integer
- Dim sCode As String
-
- Application.ScreenUpdating = False
- sCodeType = "PM001"
- Set dicData = CreateObject("Scripting.Dictionary")
- For nRow = Asc("A") To Asc("Z")
- For nI = Asc("A") To Asc("Z")
- dicData(Chr(nRow) & Chr(nI)) = 0
- Next
- Next
- With Sheet4
- vData = .[A1].CurrentRegion.Value
- If IsArray(vData) Then
- For nRow = 2 To UBound(vData)
- If vData(nRow, 1) Like sCodeType & "*" Then
- sCode = Right(vData(nRow, 1), 2)
- If dicData.Exists(sCode) Then dicData.Remove sCode
- End If
- Next
- End If
- If dicData.Count > 0 Then
- nRow = Int(Rnd() * dicData.Count)
- If nRow = dicData.Count And nRow > 0 Then nRow = nRow - 1
- sCode = sCodeType & dicData.Keys()(nRow)
- .Cells(.Cells.Rows.Count, 1).End(xlUp).Offset(1) = sCode
- MsgBox "新编号是【" & sCode & "】"
- Else
- MsgBox "该编号已经用完!"
- End If
- End With
- Application.ScreenUpdating = True
- End Sub
- Sub 顺序生成编号(ByVal sCodeType As String)
- Dim vData As Variant, nRow As Integer, nI As Integer
- Dim sCode As String
-
- Application.ScreenUpdating = False
- sCodeType = "PM001"
- With Sheet4
- vData = .[A1].CurrentRegion.Value
- If IsArray(vData) Then
- For nRow = 2 To UBound(vData)
- If vData(nRow, 1) Like sCodeType & "*" Then
- If Right(vData(nRow, 1), 2) > sCode Then sCode = Right(vData(nRow, 1), 2)
- End If
- Next
- End If
- If sCode = "ZZ" Then
- MsgBox "该编号已经用完!"
- Else
- If sCode = "" Then
- sCode = sCodeType & "AA"
- Else
- nRow = Asc(Left(sCode, 1))
- nI = Asc(Right(sCode, 1))
- If nI < Asc("Z") Then
- nI = nI + 1
- sCode = sCodeType & Left(sCode, 1) & Chr(nI)
- Else
- sCode = sCodeType & Chr(Asc(Left(sCode, 1)) + 1) & "A"
- End If
- End If
- .Cells(.Cells.Rows.Count, 1).End(xlUp).Offset(1) = sCode
- MsgBox "新编号是【" & sCode & "】"
- End If
- End With
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|