Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel macro - mark duplicates

Posted on 2000-02-21
6
Medium Priority
?
698 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
Technology Partners: 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!

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

636 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