Solved

Excel macro - mark duplicates

Posted on 2000-02-21
6
693 Views
Last Modified: 2012-06-22
I got some help on this before but require a little more.  I have a sheet where upon opening was prompting for an SOP and then checking for duplicates using the code below.  I don't want the prompt for SOP anymore but instead I want the change to occur on the fly.  For example if the person has f-21 in column C somewhere and later they enter the same, I want both rows to be highlighted in red.

Dim rngSOP As Range
  Dim intCurrentCell As Integer
   
  Set rngSOP = Range("C3", "C65536")
  rngSOP.Select
  With rngSOP
    Set fnd = .Find(strSOP)
    If fnd Is Nothing Then
      .End(xlDown).Cells.Select
      intCurrentCell = ActiveCell.Row + 1
      Range("C" & intCurrentCell).Select
      With Selection
        .Value = strSOP
      End With
    Else
      .End(xlDown).Cells.Select
      intCurrentCell = ActiveCell.Row + 1
      Range("C" & intCurrentCell).Select
      With Selection
        .Value = strSOP
        With .Interior
          .ColorIndex = 48
          .Pattern = xlSolid
          .PatternColorIndex = xlAutomatic
        End With
      End With
      MsgBox "Duplicate entry entered!", vbExclamation
    End If
  End With


0
Comment
Question by:treyjeff
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:calacuccia
ID: 2543744
Hi Treyjeff,

When you put this code on the Private Worksheet_Change event the sheet you want the duplicate rows to be highlighted in red, it should work, providing the new entries are on the last used row. To get to the worksheet events, right-click on the worksheet you want this to happen in the VBA project explorer and right-click, select view code.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Set strSOP = Target
Dim rngSOP As Range
  Dim intCurrentCell As Integer
  Set rngSOP = Range("C3", "C" & Range("C3").End(xlDown).Row - 1)
  With rngSOP
    Set fnd = .Find(strSOP)
    intCurrentCell = Target.Row
    If Not fnd Is Nothing Then
      RowNu = fnd.Row
      With Rows(intCurrentCell).Interior
          .ColorIndex = 3
          .Pattern = xlSolid
          .PatternColorIndex = xlAutomatic
        End With
       With Rows(RowNu).Interior
          .ColorIndex = 3
          .Pattern = xlSolid
          .PatternColorIndex = xlAutomatic
       End With
      MsgBox "Duplicate entry entered!", vbExclamation
    End If
  End With
End Sub

Hope this helps

Calacuccia
0
 
LVL 1

Author Comment

by:treyjeff
ID: 2547550
The very first entry i enter returns a duplicate even though it's the first entry in the shhet.
0
 
LVL 1

Author Comment

by:treyjeff
ID: 2547604
Ok, I enter data in column A and B on the second row of entries (row 5) and it highlights a row.  I've zipped it as tammy.zip on my website for you to take a look.  I don't know why it's causing problems :)  The person that enters this will start from column A and head right.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:treyjeff
ID: 2547611
Address might be good! http://www.hyrum.net/tammy.zip
0
 
LVL 17

Accepted Solution

by:
calacuccia earned 30 total points
ID: 2548253
Hi Treyjeff,

From the file on your webside, I've made some small modifications. You're perfectly right that the macro would not work for the first entry. That's because the lookup range for the newly entered value includes the value itself. A second fault was that entries in other columns were also compare to the contents of column C, so if you had a number 5 in column B, and column C contained for example some number with a 5 in, the macro found a match.

Following sub should work better:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Set strSOP = Target
Dim rngSOP As Range
  Dim intCurrentCell As Integer
  Set rngSOP = Range("C3", "C" & Range("C3").End(xlDown).Row - 1)
' Following line controls if the actual column is
' column C and if you're not doing the first entry (row 3)
  If Target.Column = 3 And Target.Row > 4 Then
  With rngSOP
' in the Find formula, xlWhole is added so that a complete match is searched for
    Set fnd = .Find(strSOP, LookAt:=xlWhole)
    intCurrentCell = Target.Row
    If Not fnd Is Nothing Then
      RowNu = fnd.Row
      With Rows(intCurrentCell).Interior
          .ColorIndex = 3
          .Pattern = xlSolid
          .PatternColorIndex = xlAutomatic
        End With
       With Rows(RowNu).Interior
          .ColorIndex = 3
          .Pattern = xlSolid
          .PatternColorIndex = xlAutomatic
       End With
      MsgBox "Duplicate entry entered!", vbExclamation
    End If
  End With
  End If
End Sub


Good Luck

Calacuccia
0
 
LVL 1

Author Comment

by:treyjeff
ID: 2550221
Thanks a lot :)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
Introduction It seems that at least a couple of times per month, I answer a question that requires automating Outlook from another Microsoft Office application, usually (although not always) to send one or more email messages.  For example: …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question