Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 135
Default expand duplicate check to multiple columns

I wrote some code to check a column for duplicates. If duplicates are found,
a message box shows that the column has duplicate values. I want to expand
the code to include multiple columns, so the message box will then list all
of the columns that have duplicates. My code checks column B - I want to also
check C through M. The data is in rows 3 - 42, and column headers are in row
2.

Here is my code:
Sub DupeCheck()
Dim r As Integer
Dim m As String

For r = 42 To 3 Step -1
If Cells(r, "B").Value = "" Then
GoTo EmptyCell
End If
If Application.WorksheetFunction.CountIf(Range("B1:B" & r), Range("B" &
r).Text) 1 Then
m = m & vbLf & Cells(2, "B").Value
Exit For
End If
EmptyCell:
Next r

MsgBox "These columns have duplicates: " & vbLf & m

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default expand duplicate check to multiple columns

I checked .value to .value in the =countif() expression. I'm not sure why you
used .text. But if you needed that, you'll have to change it back:

Option Explicit
Sub DupeCheck()
Dim iRow As Long
Dim iCol As Long
Dim m As String

m = ""
With ActiveSheet
For iCol = .Range("B1").Column To .Range("M1").Column
For iRow = 42 To 3 Step -1
If .Cells(iRow, iCol).Value = "" Then
'skip it
Else
If Application.CountIf(.Cells(1, iCol).Resize(iRow, 1), _
.Cells(iRow, iCol).Value) 1 Then
m = m & vbLf & .Cells(2, iCol).Value
Exit For
End If
End If
Next iRow
Next iCol
End With

If m = "" Then
MsgBox "no duplicates"
Else
MsgBox "These columns have duplicates: " & vbLf & m
End If

End Sub

You may want to look at some of the techniques Chip Pearson uses to highlight
duplicates.
http://www.cpearson.com/excel/Duplicates.aspx


Horatio J. Bilge, Jr. wrote:

I wrote some code to check a column for duplicates. If duplicates are found,
a message box shows that the column has duplicate values. I want to expand
the code to include multiple columns, so the message box will then list all
of the columns that have duplicates. My code checks column B - I want to also
check C through M. The data is in rows 3 - 42, and column headers are in row
2.

Here is my code:
Sub DupeCheck()
Dim r As Integer
Dim m As String

For r = 42 To 3 Step -1
If Cells(r, "B").Value = "" Then
GoTo EmptyCell
End If
If Application.WorksheetFunction.CountIf(Range("B1:B" & r), Range("B" &
r).Text) 1 Then
m = m & vbLf & Cells(2, "B").Value
Exit For
End If
EmptyCell:
Next r

MsgBox "These columns have duplicates: " & vbLf & m

End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 135
Default expand duplicate check to multiple columns

That works great. Why I used .text -- I found a Delete_Dupes macro, and
tweaked it for my needs. That macro used the .text, so I left it that way.
Using .value makes more sense, though.

Thanks for the help!
~ Horatio


"Dave Peterson" wrote:

I checked .value to .value in the =countif() expression. I'm not sure why you
used .text. But if you needed that, you'll have to change it back:

Option Explicit
Sub DupeCheck()
Dim iRow As Long
Dim iCol As Long
Dim m As String

m = ""
With ActiveSheet
For iCol = .Range("B1").Column To .Range("M1").Column
For iRow = 42 To 3 Step -1
If .Cells(iRow, iCol).Value = "" Then
'skip it
Else
If Application.CountIf(.Cells(1, iCol).Resize(iRow, 1), _
.Cells(iRow, iCol).Value) 1 Then
m = m & vbLf & .Cells(2, iCol).Value
Exit For
End If
End If
Next iRow
Next iCol
End With

If m = "" Then
MsgBox "no duplicates"
Else
MsgBox "These columns have duplicates: " & vbLf & m
End If

End Sub

You may want to look at some of the techniques Chip Pearson uses to highlight
duplicates.
http://www.cpearson.com/excel/Duplicates.aspx


Horatio J. Bilge, Jr. wrote:

I wrote some code to check a column for duplicates. If duplicates are found,
a message box shows that the column has duplicate values. I want to expand
the code to include multiple columns, so the message box will then list all
of the columns that have duplicates. My code checks column B - I want to also
check C through M. The data is in rows 3 - 42, and column headers are in row
2.

Here is my code:
Sub DupeCheck()
Dim r As Integer
Dim m As String

For r = 42 To 3 Step -1
If Cells(r, "B").Value = "" Then
GoTo EmptyCell
End If
If Application.WorksheetFunction.CountIf(Range("B1:B" & r), Range("B" &
r).Text) 1 Then
m = m & vbLf & Cells(2, "B").Value
Exit For
End If
EmptyCell:
Next r

MsgBox "These columns have duplicates: " & vbLf & m

End Sub


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 135
Default expand duplicate check to multiple columns

I used one of Chip Pearson's highlighting techniques in a different project,
but I wanted to go a different route for this one.


"Dave Peterson" wrote:

I checked .value to .value in the =countif() expression. I'm not sure why you
used .text. But if you needed that, you'll have to change it back:

Option Explicit
Sub DupeCheck()
Dim iRow As Long
Dim iCol As Long
Dim m As String

m = ""
With ActiveSheet
For iCol = .Range("B1").Column To .Range("M1").Column
For iRow = 42 To 3 Step -1
If .Cells(iRow, iCol).Value = "" Then
'skip it
Else
If Application.CountIf(.Cells(1, iCol).Resize(iRow, 1), _
.Cells(iRow, iCol).Value) 1 Then
m = m & vbLf & .Cells(2, iCol).Value
Exit For
End If
End If
Next iRow
Next iCol
End With

If m = "" Then
MsgBox "no duplicates"
Else
MsgBox "These columns have duplicates: " & vbLf & m
End If

End Sub

You may want to look at some of the techniques Chip Pearson uses to highlight
duplicates.
http://www.cpearson.com/excel/Duplicates.aspx


Horatio J. Bilge, Jr. wrote:

I wrote some code to check a column for duplicates. If duplicates are found,
a message box shows that the column has duplicate values. I want to expand
the code to include multiple columns, so the message box will then list all
of the columns that have duplicates. My code checks column B - I want to also
check C through M. The data is in rows 3 - 42, and column headers are in row
2.

Here is my code:
Sub DupeCheck()
Dim r As Integer
Dim m As String

For r = 42 To 3 Step -1
If Cells(r, "B").Value = "" Then
GoTo EmptyCell
End If
If Application.WorksheetFunction.CountIf(Range("B1:B" & r), Range("B" &
r).Text) 1 Then
m = m & vbLf & Cells(2, "B").Value
Exit For
End If
EmptyCell:
Next r

MsgBox "These columns have duplicates: " & vbLf & m

End Sub


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Indicate Duplicate Entries Across Multiple Columns Cameron Excel Discussion (Misc queries) 6 January 17th 09 12:23 AM
Using Lookups to check multiple Columns Bombay Excel Worksheet Functions 1 October 9th 08 03:46 AM
How do I recognize duplicate cells in multiple columns? cp mccall Excel Discussion (Misc queries) 1 August 22nd 06 04:32 AM
How to have multiple columns in excel that will expand and colaps. columnhelp Excel Discussion (Misc queries) 1 April 5th 05 11:42 PM
Using COUNTIF to check values in multiple columns DTomSimpson Excel Worksheet Functions 2 March 29th 05 04:47 AM


All times are GMT +1. The time now is 02:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"