VBA表单-如果范围内仅存在一次,则可能更改文本框值

VBA表单-如果范围内仅存在一次,则可能更改文本框值,vba,excel,Vba,Excel,我正在使用“保存”和“更改”按钮添加/更改范围A:A中的订单号。如果我输入新订单号(“保存”按钮),则该按钮有效,但如果我要更改范围A:A中已存在的旧订单号(“更改”按钮),则该按钮无效 Private Sub CommandButton2_Click() 'Change Button Dim sonsat As Long If ListBox1.ListIndex = -1 Then MsgBox "Select ", vbExclamation Exit S

我正在使用“保存”和“更改”按钮添加/更改范围A:A中的订单号。如果我输入新订单号(“保存”按钮),则该按钮有效,但如果我要更改范围A:A中已存在的旧订单号(“更改”按钮),则该按钮无效

    Private Sub CommandButton2_Click() 'Change Button
Dim sonsat As Long

    If ListBox1.ListIndex = -1 Then
    MsgBox "Select ", vbExclamation
    Exit Sub
    End If
    Sheets("orders").Range("A:A").Find(ListBox1.Text).Activate
    sonsat = ActiveCell.Row

        If Not Columns(1).Find(Me.TextBox1.Value, , , xlWhole, , , False) Is Nothing Then
            MsgBox Me.TextBox1.Value & " Already used ", , "Same number"
            Cancel = True
            Exit Sub
        End If


    Cells(sonsat, 1) = TextBox1
    Cells(sonsat, 2) = TextBox2
    Cells(sonsat, 3) = CDbl(TextBox3)
    Cells(sonsat, 4) = Format(TextBox4, "d.m.yyyy")
    Cells(sonsat, 5) = Format(TextBox5, "d.m.yyyy")
    Cells(sonsat, 6) = CDbl(TextBox6)


    Call Main 'Progress Bar
    MsgBox "Changed"
    ListBox1.List = Sheets("orders").Range("a2:l" & [a65536].End(3).Row).Value 'For refresh listbox
    End Sub

我需要更新此代码,如果在范围A:A中只存在一次,则允许我更改订单号。这些订单号由用户通过文本框(VBA表单)输入。

该代码放在哪个子/函数中?什么是“取消”?我只需要在更改代码之前检查一次范围内的所有数字。添加数据示例:“之前”和“之后”场景“那代码放在哪个子功能中?”?什么是“取消”?我只需要在更改代码之前检查一次范围内的所有数字。添加数据示例:“之前”和“之后”场景即可