Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Remove duplicates

I would like to create a macro that would work only on coulomb A. Coulomb A
can contain duplicate names(such as music artists). I would like to clear
the contents of all the duplicate names and then set the original name in
BOLD.
Thanks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Remove duplicates

Hi Stanley,

Make sure you backup your workbook before running this code because it is
deleting rows and you need to confirm that it is doing what you want it to.
(I have assumed that you want to delete the entire row so if this is not
correct then please get back to me.)

The code inserts a temporary column B to mark the rows to be deleted and
then runs the deletions later and removes the temporary column B.

The code runs on the active sheet so you also need to ensure that the sheet
to be processed is the active sheet before running the code.

Sub RemoveDuplicates()

Dim rngToTest As Range
Dim rngTofind As Range
Dim cel As Range
Dim firstAddress As String
Dim i As Long
Dim rngSave As Range

With ActiveSheet
Set rngToTest = Range(.Cells(2, "A"), _
Cells(.Rows.Count, "A").End(xlUp))
End With

'Insert a temporary column to hold delete flag
Columns("B:B").Insert Shift:=xlToRight

Application.ScreenUpdating = False

For Each rngTofind In rngToTest

'Test if value already searched
If rngTofind.Offset(0, 1) < "Delete" Then
Set cel = Columns("A:A").Find(What:=rngTofind.Value, _
After:=Cells(Rows.Count, "A"), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

Set rngSave = Nothing
If Not cel Is Nothing Then 'Found
firstAddress = cel.Address
Set rngSave = cel 'Save the location
Set cel = rngToTest.FindNext(cel)
Do While Not cel Is Nothing And _
cel.Address < firstAddress
cel.Offset(0, 1) = "Delete"
rngSave.Font.Bold = True 'Bold original find
Set cel = rngToTest.FindNext(cel)
Loop
End If
End If
Next rngTofind

With rngToTest
'Work backwards when deleting rows
For i = .Rows.Count To 1 Step -1
If .Cells(i, 1).Offset(0, 1) = "Delete" Then
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With

'Delete the temporary column
Columns("B:B").Delete
Application.ScreenUpdating = True

End Sub

--
Regards,

OssieMac


"Stanley Braverman" wrote:

I would like to create a macro that would work only on coulomb A. Coulomb A
can contain duplicate names(such as music artists). I would like to clear
the contents of all the duplicate names and then set the original name in
BOLD.
Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Remove duplicates

(I have assumed that you want to delete the entire row so if this is not
Correct then please get back to me.)

Actually no. I have 3 additional coulombs and would like to keep the
contents intact.
I would need the deleted cells just to be left clear. Other then that the
code runs well without errors.
Thanks


(I have assumed that you want to delete the entire row so if this is not
correct then please get back to me.)

The code inserts a temporary column B to mark the rows to be deleted and
then runs the deletions later and removes the temporary column B.

The code runs on the active sheet so you also need to ensure that the
sheet
to be processed is the active sheet before running the code.

Sub RemoveDuplicates()

Dim rngToTest As Range
Dim rngTofind As Range
Dim cel As Range
Dim firstAddress As String
Dim i As Long
Dim rngSave As Range

With ActiveSheet
Set rngToTest = Range(.Cells(2, "A"), _
Cells(.Rows.Count, "A").End(xlUp))
End With

'Insert a temporary column to hold delete flag
Columns("B:B").Insert Shift:=xlToRight

Application.ScreenUpdating = False

For Each rngTofind In rngToTest

'Test if value already searched
If rngTofind.Offset(0, 1) < "Delete" Then
Set cel = Columns("A:A").Find(What:=rngTofind.Value, _
After:=Cells(Rows.Count, "A"), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

Set rngSave = Nothing
If Not cel Is Nothing Then 'Found
firstAddress = cel.Address
Set rngSave = cel 'Save the location
Set cel = rngToTest.FindNext(cel)
Do While Not cel Is Nothing And _
cel.Address < firstAddress
cel.Offset(0, 1) = "Delete"
rngSave.Font.Bold = True 'Bold original find
Set cel = rngToTest.FindNext(cel)
Loop
End If
End If
Next rngTofind

With rngToTest
'Work backwards when deleting rows
For i = .Rows.Count To 1 Step -1
If .Cells(i, 1).Offset(0, 1) = "Delete" Then
.Cells(i, 1).EntireRow.Delete
End If
Next i
End With

'Delete the temporary column
Columns("B:B").Delete
Application.ScreenUpdating = True

End Sub

--
Regards,

OssieMac


"Stanley Braverman" wrote:

I would like to create a macro that would work only on coulomb A. Coulomb
A
can contain duplicate names(such as music artists). I would like to clear
the contents of all the duplicate names and then set the original name in
BOLD.
Thanks





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Remove duplicates

OK Stanley done. It just clears the cells now instead of deleting the rows.
Have also modified a couple of other bits that makes the code more generic
therefore replace all of the code.

Sub RemoveDuplicates()

Dim rngToTest As Range
Dim rngTofind As Range
Dim cel As Range
Dim firstAddress As String
Dim i As Long
Dim rngSave As Range

With ActiveSheet
Set rngToTest = Range(.Cells(2, "A"), _
.Cells(.Rows.Count, "A").End(xlUp))

'Insert a temporary column to hold delete flag
.Columns("B:B").Insert Shift:=xlToRight

End With

Application.ScreenUpdating = False

For Each rngTofind In rngToTest

'Test if value already searched
With rngToTest
If rngTofind.Offset(0, 1) < "Delete" Then
Set cel = .Columns("A:A").Find(What:=rngTofind.Value, _
After:=.Cells(.Rows.Count, "A"), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

Set rngSave = Nothing
If Not cel Is Nothing Then 'Found
firstAddress = cel.Address
Set rngSave = cel 'Save the location
Set cel = .FindNext(cel)
Do While Not cel Is Nothing And _
cel.Address < firstAddress
cel.Offset(0, 1) = "Delete"
rngSave.Font.Bold = True 'Bold original find
Set cel = .FindNext(cel)
Loop
End If
End If
End With
Next rngTofind

With rngToTest
'Work backwards when deleting rows
For i = .Rows.Count To 1 Step -1
If .Cells(i, 1).Offset(0, 1) = "Delete" Then
.Cells(i, 1).Clear
End If
Next i
End With

'Delete the temporary column
With ActiveSheet
.Columns("B:B").Delete
End With

Application.ScreenUpdating = True

End Sub


--
Regards,

OssieMac


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Remove duplicates

Thanks OssieMac,
This works great.
Regards, Stan
"OssieMac" wrote in message
...
OK Stanley done. It just clears the cells now instead of deleting the
rows.
Have also modified a couple of other bits that makes the code more generic
therefore replace all of the code.

Sub RemoveDuplicates()

Dim rngToTest As Range
Dim rngTofind As Range
Dim cel As Range
Dim firstAddress As String
Dim i As Long
Dim rngSave As Range

With ActiveSheet
Set rngToTest = Range(.Cells(2, "A"), _
.Cells(.Rows.Count, "A").End(xlUp))

'Insert a temporary column to hold delete flag
.Columns("B:B").Insert Shift:=xlToRight

End With

Application.ScreenUpdating = False

For Each rngTofind In rngToTest

'Test if value already searched
With rngToTest
If rngTofind.Offset(0, 1) < "Delete" Then
Set cel = .Columns("A:A").Find(What:=rngTofind.Value, _
After:=.Cells(.Rows.Count, "A"), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

Set rngSave = Nothing
If Not cel Is Nothing Then 'Found
firstAddress = cel.Address
Set rngSave = cel 'Save the location
Set cel = .FindNext(cel)
Do While Not cel Is Nothing And _
cel.Address < firstAddress
cel.Offset(0, 1) = "Delete"
rngSave.Font.Bold = True 'Bold original find
Set cel = .FindNext(cel)
Loop
End If
End If
End With
Next rngTofind

With rngToTest
'Work backwards when deleting rows
For i = .Rows.Count To 1 Step -1
If .Cells(i, 1).Offset(0, 1) = "Delete" Then
.Cells(i, 1).Clear
End If
Next i
End With

'Delete the temporary column
With ActiveSheet
.Columns("B:B").Delete
End With

Application.ScreenUpdating = True

End Sub


--
Regards,

OssieMac




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
Remove duplicates jennifer Excel Worksheet Functions 2 August 28th 09 07:26 PM
remove duplicates need help faceliftguide Setting up and Configuration of Excel 3 July 14th 09 02:27 PM
Remove Duplicates Joe Excel Worksheet Functions 2 February 13th 09 11:58 PM
How to remove duplicates? Lakewoodsale Excel Discussion (Misc queries) 2 January 25th 08 10:31 PM
Remove duplicates Tuttamay77 Excel Discussion (Misc queries) 4 May 12th 06 10:56 PM


All times are GMT +1. The time now is 01:30 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"