-=Scan and replace=-

TAPAKAH

New Member
Joined
Apr 16, 2004
Messages
9
Hi guys,

Trying to do the basic search and destroy, but with a little modification.
I have one column where values repeat:

Example:

1
2
4
5
1

So a macro would start at the top, look at first row, and then go down the column and look if there is same value somewhere down the column and if found replace it with a 0. Then it would come back go to the second row and do the same untill the end.

I would really appreciate any help.

Thanks in advance.

:)
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

vconfused

Well-known Member
Joined
Jun 11, 2004
Messages
547
Something like this?

Code:
Sub SearchandDestroy()
Const strColumn As String = "A"
Dim cell As Range, cell2 As Range, lngLastRow As Long

    Application.ScreenUpdating = False
    lngLastRow = Range(strColumn & 65536).End(xlUp).Row
    For Each cell In Range(strColumn & 1 & ":" & strColumn & lngLastRow - 1)
        For Each cell2 In Range(cell.Offset(1, 0).Address & ":" & strColumn & lngLastRow)
            If cell = cell2 Then cell2 = 0
        Next
    Next
    Application.ScreenUpdating = True

End Sub
 

chiello

Well-known Member
Joined
Jan 18, 2005
Messages
848
Another possible solution.... :cool:

Suppose you have data in column A staring from A2 (column header in A1).
The following code should suit your request.

It copies a unique copy of data soon after the last row of data and delete above data.

Sub ScanAndReplace()
Dim MyRange As Range
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
If Range("A2") <> "" And Range("A3") <> "" Then
Set MyRange = Range(Range("A2"), Range("A2").End(xlDown))
Range(Range("A1"), Range("A2").End(xlDown)).AdvancedFilter Action:=xlFilterInPlace, Unique:=True
MyRange.SpecialCells(xlCellTypeVisible).Copy Destination:=Cells(MyRange.Rows.Count + 2, 1)
ActiveSheet.ShowAllData
MyRange.Rows.Delete shift:=xlUp
End If
End Sub
 

chiello

Well-known Member
Joined
Jan 18, 2005
Messages
848

ADVERTISEMENT

Sorry... :eek: :eek:

I didn't read to replace with 0

:oops: :oops:
 

TAPAKAH

New Member
Joined
Apr 16, 2004
Messages
9
is it possible to tell the macro to not match the case(lower or upper) of the text i believe it is "MatchCase:=False".... but i am not sure where to put it.
 

chiello

Well-known Member
Joined
Jan 18, 2005
Messages
848
Replace statement

If cell = cell2 Then cell2 = 0

with

If LCase(cell) = LCase(cell2) Then cell2 = 0

Ciao
 

Forum statistics

Threads
1,148,529
Messages
5,747,230
Members
424,070
Latest member
smanni3

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
Top