源地址


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    If sbFindDuplicatesInColumn() Then
    
    End If
    
End Sub

Private Function sbFindDuplicatesInColumn()
    Dim lastRow As Long
    Dim matchFoundIndex As Long
    Dim iCntr As Long
    lastRow = Range("A65000").End(xlUp).Row
    sbFindDuplicatesInColumn = False
    For iCntr = 1 To lastRow
    If Cells(iCntr, 1) <> "" Then
        matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
        If iCntr <> matchFoundIndex Then
            MsgBox firstSheet & " 第" & iCntr & "行 相同主键的记录已经存在!"
            sbFindDuplicatesInColumn = True
       End If
    End If
    Next
End Function

相同出现,在保存时就会出现这样的提示

/Icarus/ServiceFiles//1/2021/8/1628153699440.png