Solved

Excel macro - mark duplicates

Posted on 2000-02-21
6
689 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Perhaps more familiar to developers who primarily use VBScript than to developers who tend to work only with Microsoft Office and Visual Basic for Applications (VBA), the Dictionary is a powerful and versatile class, and is useful …
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
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 a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now