COde生成顺序号每次前缀更改时

时间:2016-12-25 08:22:03

标签: excel-vba vba excel

我有一个用户表单,其图像如下所示。

enter image description here

还附有数据输入表图像。

enter image description here

命令按钮 代码

Private Sub CommandButton1_Click()

Dim ws As Worksheet, tbl As ListObject, row As ListRow

Set ws = Sheets("Sheet1")
Set tbl = ws.ListObjects("Table2")
Set row = tbl.ListRows.Add

prefix = Me.TextBox1.Value & "-"

Dim NextNum As Long, LastRow As Long, lRow As Long, myArr() As Long

With Sheets("Sheet1")
    'Find Last Row in Voucher # Column
    LastRow = .Cells(.Rows.Count, "E").End(xlUp).row

    ReDim myArr(1 To LastRow)
    ' read all cells contents and convert them to array of numbers
    For lRow = 5 To LastRow
        If Mid(.Cells(lRow, 5), 4) <> "" Then
            myArr(lRow) = CLng(Mid(.Cells(lRow, 5), 4))
        End If
    Next lRow

    ' find maximum value in array
    NextNum = WorksheetFunction.Max(myArr)
End With
row.Range(1, 1).Value = Me.ComboBox1.Value
row.Range(1, 2).Value = prefix & NextNum + 1
row.Range(1, 3).Value = Me.TextBox2.Value

End Sub

当我按下命令按钮时,它将生成带有前缀的序列号。但是我希望在更改前缀时,它应该读取附加了该前缀的数字,然后生成下一个数字。

附上样本结果。 enter image description here

0 个答案:

没有答案