- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 2# luhpro
另一寫法
UserForm模組- Option Explicit
- Dim D As Object
- Private Sub UserForm_Initialize()
- txt編號 = 編號
- End Sub
- Private Sub cb輸入_Click()
- With Sheets("工作表1")
- If Application.CountIf(.Range("a:a"), txt編號) > 0 And txt編號 <> "" Then
- MsgBox "編號已存在!", vbCritical, "錯誤"
- Exit Sub
- ElseIf UBound(Split(txt編號, "-")) = 0 Or txt編號 = "" Then
- MsgBox "編號 格式 錯誤!", vbCritical, "錯誤"
- Exit Sub
- ElseIf UCase(txt編號) <> "AL-" & Format(Application.Max(D.KEYS) + 1, "000") Then
- MsgBox txt編號 & " 需是 " & "AL-" & Format(Application.Max(D.KEYS) + 1, "000"), vbCritical, "錯誤"
- Exit Sub
- End If
- .Range("A65536").End(xlUp).Cells(2) = txt編號
- D(Application.Max(D.KEYS) + 1) = ""
- End With
- End Sub
- Private Sub CommandButton2_Click() 'cb輸入後,可繼續 + 1
- txt編號 = "AL-" & Format(Application.Max(D.KEYS) + 1, "000")
- End Sub
- Private Function 編號() As String
- Dim E As Range
- Set D = CreateObject("SCRIPTING.DICTIONARY")
- For Each E In 工作表1.Range("A:A").SpecialCells(xlCellTypeConstants)
- If UBound(Split(E, "-")) > 0 Then D(Val(Split(E, "-")(1))) = ""
- Next
- If D.Count = 0 Then
- 編號 = "AL-001"
- D(0) = ""
- Else
- 編號 = "AL-" & Format(Application.Max(D.KEYS) + 1, "000")
- End If
- End Function
複製代碼 |
|