Solved

Excel macro - mark duplicates

Posted on 2000-02-21
6
696 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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
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…

688 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