?
Solved

Excel macro - mark duplicates

Posted on 2000-02-21
6
Medium Priority
?
697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 120 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

765 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