VBA - Problem Highlighting Duplicate Values

beartooth91

New Member
Joined
Dec 15, 2024
Messages
9
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
VBA Code:
Sub Check_Duplicates3()
'
Dim ws As String, i As Long, FinalRow As Long, Main As Worksheet, rr As Range, r As Range

ws = InputBox(Prompt:="Enter the worksheet name to check", _

Title:="Enter Worksheet Name", Default:="AST")


Set Main = ThisWorkbook.Sheets(ws)

FinalRow = Main.Range("B" & Rows.Count).End(xlUp).Row

Set rr = Main.Range("B11:B" & FinalRow)


For Each r In rr

If WorksheetFunction.CountIf(rr, r.Value) > 1 Then

   r.EntireRow.Interior.ColorIndex = 6

   MsgBox ("You have duplicate point names. Please review the highlighted rows.")

Else

   MsgBox ("No duplicate point names found.")

   GoTo LastLine

End If

Next r

LastLine:

Main.Activate

End Sub
Stumped on this one! The idea is to check for and highlight Column B duplicate names in whatever sheet the user specifies.
It works perfectly when the Else block is NOT present.
When I include the Else block; it jumps right to that block, even if there are duplicate values.
Tried some different things with no success. I'm not sure what I'm doing wrong. Should be a standard If-Then-Else statement......?

 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about
VBA Code:
Sub Check_Duplicates3()
'
Dim ws As String, i As Long, FinalRow As Long, Main As Worksheet, rr As Range, r As Range, c As Long

ws = InputBox(Prompt:="Enter the worksheet name to check", _
    Title:="Enter Worksheet Name", Default:="AST")


Set Main = ThisWorkbook.Sheets(ws)

FinalRow = Main.Range("B" & Rows.Count).End(xlUp).Row

Set rr = Main.Range("B11:B" & FinalRow)


For Each r In rr
    
    If WorksheetFunction.CountIf(rr, r.Value) > 1 Then
    
       r.EntireRow.Interior.ColorIndex = 6
    
       c = c = 1
    End If

Next r

If c > 0 Then
   MsgBox ("You have duplicate point names. Please review the highlighted rows.")
Else

   MsgBox ("No duplicate point names found.")

End If

Main.Activate

End Sub
 
Upvote 0
How about
VBA Code:
Sub Check_Duplicates3()
'
Dim ws As String, i As Long, FinalRow As Long, Main As Worksheet, rr As Range, r As Range, c As Long

ws = InputBox(Prompt:="Enter the worksheet name to check", _
    Title:="Enter Worksheet Name", Default:="AST")


Set Main = ThisWorkbook.Sheets(ws)

FinalRow = Main.Range("B" & Rows.Count).End(xlUp).Row

Set rr = Main.Range("B11:B" & FinalRow)


For Each r In rr
   
    If WorksheetFunction.CountIf(rr, r.Value) > 1 Then
   
       r.EntireRow.Interior.ColorIndex = 6
   
       c = c = 1
    End If

Next r

If c > 0 Then
   MsgBox ("You have duplicate point names. Please review the highlighted rows.")
Else

   MsgBox ("No duplicate point names found.")

End If

Main.Activate

End Sub
It works great if you change your line, above:
VBA Code:
c = c = 1
to
VBA Code:
c = c + 1
Thank You!
I understand why your version works.... I don't really understand why my version didn't work....?
 
Upvote 0
Your version will always fail if the first cell (ie B11) is not a duplicate, as the Else section tells it to exit the loop, so no other cells are checked.
 
Upvote 0

Forum statistics

Threads
1,224,748
Messages
6,180,721
Members
452,995
Latest member
isldboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top