Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 6228|回复: 30

[已解决]禁止输入重复数据的VBA代码求助

[复制链接]
发表于 2016-1-8 08:59 | 显示全部楼层 |阅读模式
本帖最后由 安全网 于 2016-1-8 16:59 编辑

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 2 And Target.Column < 9 Then
        W = Target.Row
        If Len(Cells(W, 3)) * Len(Cells(W, 4)) * Len(Cells(W, 5)) * Len(Cells(W, 8)) <> 0 And Target.Count = 1 Then
            Application.EnableEvents = False
            ARR = Range("C1:H" & Range("C65536").End(3).Row - 1)
            For I = 1 To UBound(ARR)
                If ARR(I, 1) = Cells(W, 3) And ARR(I, 2) = Cells(W, 4) And ARR(I, 3) = Cells(W, 5) And ARR(I, 6) = Cells(W, 8) Then
                    Range("C" & I & ":H" & I).Interior.ColorIndex = 6
                    If MsgBox("输入数据与第 " & I & " 行相同!是否需要删除?", 4 + 32 + 256) = 6 Then
                        Rows(W).Delete
                        Range("C2:H" & I).Interior.ColorIndex = 0
                    End If
                    'Exit For
                End If
            Next
            Application.EnableEvents = True
        End If
    End If
End Sub这个代码提示了输入重复数据时提示,但要修改已经输入的数据时,就会提示与本行数据重复
最佳答案
2016-1-8 16:50
本帖最后由 zjdh 于 2016-1-8 16:53 编辑

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 2 And Target.Column < 9 Then
        W = Target.Row
        If Len(Cells(W, 3)) * Len(Cells(W, 4)) * Len(Cells(W, 5)) * Len(Cells(W, 8)) <> 0 And Target.Count = 1 Then
            Application.EnableEvents = False
            Arr = Range("C1:H" & Range("C65536").End(3).Row )
            For I = 1 To UBound(Arr)
                If Arr(I, 1) = Cells(W, 3) And Arr(I, 2) = Cells(W, 4) And Arr(I, 3) = Cells(W, 5) And Arr(I, 6) = Cells(W, 8) Then
                    If Target.Row = I Then GoTo 10
                    Range("C" & I & ":H" & I).Interior.ColorIndex = 6
                    If MsgBox("输入数据与第 " & I & " 行相同!是否需要删除?", 4 + 32 + 256) = 6 Then
                        Rows(W).Delete
                        Range("C2:H" & I).Interior.ColorIndex = 0
                    End If
                    'Exit For
                End If
            Next
        End If
    End If
10  Application.EnableEvents = True
End Sub
 楼主| 发表于 2016-1-8 09:13 | 显示全部楼层
对于已经输入的数据,我需要修改就会出现这样的提示,提示跟本行不重复,。
QQ图片20160108090543.png
回复

使用道具 举报

发表于 2016-1-8 16:50 | 显示全部楼层    本楼为最佳答案   
本帖最后由 zjdh 于 2016-1-8 16:53 编辑

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 2 And Target.Column < 9 Then
        W = Target.Row
        If Len(Cells(W, 3)) * Len(Cells(W, 4)) * Len(Cells(W, 5)) * Len(Cells(W, 8)) <> 0 And Target.Count = 1 Then
            Application.EnableEvents = False
            Arr = Range("C1:H" & Range("C65536").End(3).Row )
            For I = 1 To UBound(Arr)
                If Arr(I, 1) = Cells(W, 3) And Arr(I, 2) = Cells(W, 4) And Arr(I, 3) = Cells(W, 5) And Arr(I, 6) = Cells(W, 8) Then
                    If Target.Row = I Then GoTo 10
                    Range("C" & I & ":H" & I).Interior.ColorIndex = 6
                    If MsgBox("输入数据与第 " & I & " 行相同!是否需要删除?", 4 + 32 + 256) = 6 Then
                        Rows(W).Delete
                        Range("C2:H" & I).Interior.ColorIndex = 0
                    End If
                    'Exit For
                End If
            Next
        End If
    End If
10  Application.EnableEvents = True
End Sub
回复

使用道具 举报

 楼主| 发表于 2016-1-8 17:01 | 显示全部楼层
zjdh 发表于 2016-1-8 16:50
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 2 And Target.Column < 9  ...

如果能实现对之前多个填充颜色的重复项,去掉其填充颜色就更加完美了。
回复

使用道具 举报

发表于 2016-1-8 17:54 | 显示全部楼层
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 2 And Target.Column < 9 Then
        W = Target.Row
        If Len(Cells(W, 3)) * Len(Cells(W, 4)) * Len(Cells(W, 5)) * Len(Cells(W, 8)) <> 0 And Target.Count = 1 Then
            Application.EnableEvents = False
            Arr = Range("C1:H" & Range("C65536").End(3).Row)
            For I = 1 To UBound(Arr)
                If Arr(I, 1) = Cells(W, 3) And Arr(I, 2) = Cells(W, 4) And Arr(I, 3) = Cells(W, 5) And Arr(I, 6) = Cells(W, 8) Then
                    If Target.Row = I Then GoTo 10
                    Range("C" & I & ":H" & I).Interior.ColorIndex = 6
                    If MsgBox("输入数据与第 " & I & " 行相同!是否需要删除?", 4 + 32 + 256) = 6 Then
                        Rows(W).Delete
                        Range("C2:H" & UBound(Arr)).Interior.ColorIndex = 0
                    End If
                    'Exit For
                End If
            Next
        End If
    End If
10  Application.EnableEvents = True
End Sub
回复

使用道具 举报

 楼主| 发表于 2016-1-9 08:35 | 显示全部楼层
zjdh 发表于 2016-1-8 17:54
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 2 And Target.Column < 9  ...

真不好意思!测试使用的时候又遇到一个这样的问题。不知道能不能解决,具体见图片
QQ图片20160109082749.png
回复

使用道具 举报

 楼主| 发表于 2016-1-9 08:36 | 显示全部楼层
安全网 发表于 2016-1-9 08:35
真不好意思!测试使用的时候又遇到一个这样的问题。不知道能不能解决,具体见图片

提示删除第8行数据,删除了第8行数据,如果第9行是空的就会出现这样的提示
回复

使用道具 举报

发表于 2016-1-9 09:51 | 显示全部楼层
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim RN As Range
    If Target.Column > 2 And Target.Column < 9 Then
        W = Target.Row
        If Len(Cells(W, 3)) * Len(Cells(W, 4)) * Len(Cells(W, 5)) * Len(Cells(W, 8)) <> 0 And Target.Count = 1 Then
            Arr = Range("C1:H" & Range("C65536").End(3).Row)
            For I = 1 To UBound(Arr)
                If Arr(I, 1) = Cells(W, 3) And Arr(I, 2) = Cells(W, 4) And Arr(I, 3) = Cells(W, 5) And Arr(I, 6) = Cells(W, 8) Then
                    If W = I Then GoTo 10
                    If RN Is Nothing Then Set RN = Range("C" & I & ":H" & I) Else Set RN = Union(RN, Range("C" & I & ":H" & I))
                    R = R & "," & I
                End If
10          Next
            Application.EnableEvents = False
            RN.Interior.ColorIndex = 6
            If MsgBox("输入数据与第 " & Mid(R, 2) & " 行相同!是否需要删除?", 4 + 32 + 256) = 6 Then
                RN.Delete
                Range("C2:H" & I).Interior.ColorIndex = 0
            End If
        End If
    End If
    Application.EnableEvents = True
End Sub
回复

使用道具 举报

 楼主| 发表于 2016-1-9 10:12 | 显示全部楼层
zjdh 发表于 2016-1-9 09:51
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim RN As Range
    If Target.Column > 2 An ...

谢谢!非常感谢!个别地方结合前面的我修改了下代码达到我需求的。
回复

使用道具 举报

 楼主| 发表于 2016-1-9 16:29 | 显示全部楼层
zjdh 发表于 2016-1-9 09:51
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim RN As Range
    If Target.Column > 2 An ...

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Row < 3 Then Exit Sub


If Target.Column = 4 Then
Dim j
If Target <> "" Then
On Error Resume Next
j = WorksheetFunction.Match(Target, Sheets("编码").Range("a:a"), 0)
    Cells(Target.Row, 6) = Sheets("编码").Cells(j, 2)
Else
    Cells(Target.Row, 6) = ""
End If
End If


If Target.Column = 2 Then
Dim n
If Target <> "" Then
    Cells(Target.Row, 7) = Format(Target, "yymm" & "月")
Else
    Cells(Target.Row, 7) = ""
End If
End If


   Dim RN As Range
    If Target.Column > 2  and Target.Column <9 Then
        W = Target.Row
        If Len(Cells(W, 3)) * Len(Cells(W, 4)) * Len(Cells(W, 5)) * Len(Cells(W, 8)) <> 0 Then
            Arr = Range("C1:H" & Range("C65536").End(3).Row)
            For I = 1 To UBound(Arr)
                If Arr(I, 1) = Cells(W, 3) And Arr(I, 2) = Cells(W, 4) And Arr(I, 3) = Cells(W, 5) And Arr(I, 6) = Cells(W, 8) Then
                    If W = I Then GoTo 10
                    If RN Is Nothing Then Set RN = Range("C" & I & ":H" & I) Else Set RN = Union(RN, Range("C" & I & ":H" & I))
                    R = R & "," & I
                End If
10          Next
            Application.EnableEvents = False
            RN.Interior.ColorIndex = 6
            If MsgBox("输入数据与第 " & Mid(R, 2) & " 行相同!是否需要删除输入的数据?", 4 + 32 + 256) = 6 Then
                Rows(W).ClearContents
                RN.Interior.ColorIndex = 0
            End If
        End If
    End If
    Application.EnableEvents = True
End Sub
加入上面红色的代码后,因为第4列由使用2次激活,就会出现图示的错误,怎么修改VBA避免错误

QQ图片20160109161758.png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-26 21:50 , Processed in 0.579208 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表