Excel VBA代码生成带前缀的顺序号

时间:2016-12-29 08:51:12

标签: excel-vba vba excel

我的代码存在一个小问题。代码如下。

Private Sub CommandButton1_Click()
Dim Rng As Range, Dn As Range, n As Long
Dim pType As String
Dim oMax As Long
Set Rng = Range(Range("E1"), Range("E" & Rows.Count).End(xlUp))
Const StartNum = 1
    pType = Me.TextBox1.Value
if pType = vbNullString Then MsgBox "Please Select Option from Combo Box": Exit Sub


With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Left(Dn.Value, 1) = pType Then
If Not .Exists(Dn.Value) Then
   .Add Dn.Value, Mid(Dn, 2)
oMax = Application.Max(.Item(Dn.Value), Mid(Dn, 2))
Else
    MsgBox "Number Exists:-" & Dn.Value
End If
End If
Next
If .Count = 0 Then
Range("E" & Rng.Count + 1) = pType & StartNum
Else
Range("E" & Rng.Count + 1) = pType & oMax + 1
End If
End With

End Sub

此代码开始从第2行生成数字,而我希望起始行应为5.我尝试将范围值从E1更改为E5,但它没有工作。

请审核并建议修改。

感谢。

1 个答案:

答案 0 :(得分:0)

您只需更改以下行

即可
Range("E" & Rng.Count + 1) = pType & StartNum

Range("E" & Rng.Count + 4) = pType & StartNum