Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 159
Default What is wrong with my code??

I copied and edited this code to find duplicate names in my worksheet column
B and then display a msgbox to tell the user so, however it is not working at
all. Please advise

Sub FindDuplicates()
Dim colNum As String
Dim rng As Range, cell As Range
Dim rng1 As Range
colNum = "B"
With ActiveSheet
Set rng = .Range(.Cells(1, colNum), .Cells(Rows.Count, colNum).End(xlUp))
End With
For Each cell In rng
If Application.CountIf(rng, cell) 1 Then
If rng1 Is Nothing Then
Set rng1 = cell
Else
Set rng1 = Union(rng1, cell)
End If
End If
Next
If Not rng1 Is Nothing Then
MsgBox "YOU HAVE ENTERED A DUPLICATE MANUFACTURER NAME"
End If
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default What is wrong with my code??

Works for me
Replace your msgbox with this

MsgBox rng1.Address

"Mekinnik" wrote:

I copied and edited this code to find duplicate names in my worksheet column
B and then display a msgbox to tell the user so, however it is not working at
all. Please advise

Sub FindDuplicates()
Dim colNum As String
Dim rng As Range, cell As Range
Dim rng1 As Range
colNum = "B"
With ActiveSheet
Set rng = .Range(.Cells(1, colNum), .Cells(Rows.Count, colNum).End(xlUp))
End With
For Each cell In rng
If Application.CountIf(rng, cell) 1 Then
If rng1 Is Nothing Then
Set rng1 = cell
Else
Set rng1 = Union(rng1, cell)
End If
End If
Next
If Not rng1 Is Nothing Then
MsgBox "YOU HAVE ENTERED A DUPLICATE MANUFACTURER NAME"
End If
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default What is wrong with my code??

Nothing I can see ;)

You could perhaps be a bit more helpful with the message box by giving the
address of the duolicate

"Mekinnik" wrote:

I copied and edited this code to find duplicate names in my worksheet column
B and then display a msgbox to tell the user so, however it is not working at
all. Please advise

Sub FindDuplicates()
Dim colNum As String
Dim rng As Range, cell As Range
Dim rng1 As Range
colNum = "B"
With ActiveSheet
Set rng = .Range(.Cells(1, colNum), .Cells(Rows.Count, colNum).End(xlUp))
End With
For Each cell In rng
If Application.CountIf(rng, cell) 1 Then
If rng1 Is Nothing Then
Set rng1 = cell
Else
Set rng1 = Union(rng1, cell)
End If
End If
Next
If Not rng1 Is Nothing Then
MsgBox "YOU HAVE ENTERED A DUPLICATE MANUFACTURER NAME"
End If
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default What is wrong with my code??

does this do what you want? i see it was exiting only after finding the 1st
match.

Sub FindDuplicates()
Dim colNum As String
Dim rng As Range, cell As Range
Dim rng1 As Range
colNum = "B"
With ActiveSheet
Set rng = .Range(.Cells(1, colNum), .Cells(Rows.Count, _
colNum).End(xlUp))
End With
For Each cell In rng
If Application.CountIf(rng, cell) 1 Then
If rng1 Is Nothing Then
Set rng1 = cell
Else
Set rng1 = Union(rng1, cell)
MsgBox "YOU HAVE ENTERED A DUPLICATE MANUFACTURER NAME"
_
& " (" & cell.Value & ")"
End If
End If
Next
End Sub
--


Gary


"Mekinnik" wrote in message
...
I copied and edited this code to find duplicate names in my worksheet column
B and then display a msgbox to tell the user so, however it is not working at
all. Please advise

Sub FindDuplicates()
Dim colNum As String
Dim rng As Range, cell As Range
Dim rng1 As Range
colNum = "B"
With ActiveSheet
Set rng = .Range(.Cells(1, colNum), .Cells(Rows.Count, colNum).End(xlUp))
End With
For Each cell In rng
If Application.CountIf(rng, cell) 1 Then
If rng1 Is Nothing Then
Set rng1 = cell
Else
Set rng1 = Union(rng1, cell)
End If
End If
Next
If Not rng1 Is Nothing Then
MsgBox "YOU HAVE ENTERED A DUPLICATE MANUFACTURER NAME"
End If
End Sub



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default What is wrong with my code??

The code work fine in excel 2003. make sure the worksheet is the active
worksheet before you run the code and make sure tthe code is in a module VBA
sheet. Close all workbooks except the active workbook


Also check that your security level to make sure it is not set to high or
very high.

"Mekinnik" wrote:

I copied and edited this code to find duplicate names in my worksheet column
B and then display a msgbox to tell the user so, however it is not working at
all. Please advise

Sub FindDuplicates()
Dim colNum As String
Dim rng As Range, cell As Range
Dim rng1 As Range
colNum = "B"
With ActiveSheet
Set rng = .Range(.Cells(1, colNum), .Cells(Rows.Count, colNum).End(xlUp))
End With
For Each cell In rng
If Application.CountIf(rng, cell) 1 Then
If rng1 Is Nothing Then
Set rng1 = cell
Else
Set rng1 = Union(rng1, cell)
End If
End If
Next
If Not rng1 Is Nothing Then
MsgBox "YOU HAVE ENTERED A DUPLICATE MANUFACTURER NAME"
End If
End Sub

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
Can someone tell me what is wrong with this code? Ant Excel Discussion (Misc queries) 8 November 14th 05 02:53 PM
What's wrong with my code ? christophe meresse Excel Worksheet Functions 3 August 2nd 05 05:09 PM
What's wrong with the code,pls hv a look changeable[_10_] Excel Programming 0 November 3rd 04 09:56 AM
What is wrong with this code? Bob Phillips[_6_] Excel Programming 1 January 22nd 04 10:11 PM
What's wrong with this bit of code Mervyn Thomas Excel Programming 7 January 22nd 04 03:56 PM


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

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

About Us

"It's about Microsoft Excel"