如何防止重复添加单元格?

时间:2021-08-12 04:01:26

标签: excel vba

我希望在添加商品时弹出一个消息框“重复项”,并且不允许用户将相同的项目添加到购物车中。

以下是我的代码,但它仅适用于购物车的第二行开始:

Sub addcart()
    Dim lastrow As Long
    Dim i As Integer, j As Integer, m As Integer
    Dim x As Variant
    lastrow = Sheets("Sales Point").Range("F" & Rows.Count).End(xlUp).Row
    x = Cells(lastrow, 7).Offset(-1, 0).Value
    If Range("C3").Value = "Please Select Option" Then
        MsgBox "请选择选项!"
    Else
        If Range("C6").Value = "请输入数量" Then
            MsgBox "请输入数量!"
        Else
            If x = Cells(lastrow, 7) Then
                MsgBox "重复条目!"

            Else
                For i = 5 To lastrow + 1
                    Cells(i, 5).Value = i - 4
                Next

                Cells(lastrow + 1, 6) = Range("C4")
                Cells(lastrow + 1, 7) = Range("C3")
                Cells(lastrow + 1, 8) = Range("C6")
                Cells(lastrow + 1, 9) = Format(Range("C5"), "Currency")
                Cells(lastrow + 1, 10) = Format(Cells(lastrow + 1, 9) * Cells(lastrow + 1, 8), "Currency")

                For m = 1 To 4
                    For j = 0 To 5
                        Range("E" & Rows.Count).End(xlUp).Offset(0, j).Font.Bold = False
                        Range("E" & Rows.Count).End(xlUp).Offset(0, j).Borders().LineStyle = xlNone
                        Range("E" & Rows.Count).End(xlUp).Offset(m, j).Clear
                    Next j
                Next m
            End If
        End If
    End If
    Worksheets("Sales Point").Columns("E:J").HorizontalAlignment = xlCenter

End Sub

这是我的工作表看起来像这样:

enter image description here

0 个答案:

没有答案