Require macro

Ron99

Active Member
Joined
Feb 10, 2010
Messages
347
Office Version
  1. 2016
Platform
  1. Windows
Hello,
I need a macro to remove duplicates by row and also remove the blanks. Its a huge data by rows and columns
below example - duplicates "Rob & "goal" to be removed, but should retain unique and remove the blanks

[TABLE="width: 500"]
<tbody>[TR]
[TD]gulf[/TD]
[TD]Rob[/TD]
[TD][/TD]
[TD][/TD]
[TD]stat[/TD]
[TD]Rob[/TD]
[TD]aim[/TD]
[TD]hero[/TD]
[TD]fun[/TD]
[TD]goal[/TD]
[TD]view[/TD]
[TD]review[/TD]
[TD][/TD]
[TD]page[/TD]
[TD]goal[/TD]
[/TR]
</tbody>[/TABLE]


Output -


[TABLE="width: 500"]
<tbody>[TR]
[TD]gulf[/TD]
[TD]Rob[/TD]
[TD]stat[/TD]
[TD]aim[/TD]
[TD]hero[/TD]
[TD]fun[/TD]
[TD]goal[/TD]
[TD][/TD]
[TD]view[/TD]
[TD]review[/TD]
[TD]page[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Here is one way. I tried to document the code so you can see what is going on.
Code:
Sub MyMacro()

    Dim lastRow As Long
    Dim r As Long
    Dim lastCol As Long
    Dim c As Long
    Dim myRange As Range
    Dim myVal
    
    Application.ScreenUpdating = False
    
'   Find last row on workbook
    lastRow = Range("A1").SpecialCells(xlLastCell).Row
    
'   Loop through all rows, starting on row 1
    For r = 1 To lastRow
'       Find last column in that row
        lastCol = Cells(r, Columns.Count).End(xlToLeft).Column
'       Loop through all columns backwards
        For c = lastCol To 1 Step -1
'           Get cell value
            myVal = Cells(r, c)
            Select Case myVal
'               Remove blanks
                Case ""
                    Cells(r, c).Delete Shift:=xlToLeft
                Case Else
'                   Remove duplicates
                    If c > 1 Then
                        Set myRange = Range(Cells(r, 1), Cells(r, c - 1))
                        If Application.WorksheetFunction.CountIf(myRange, myVal) > 0 Then
                            Cells(r, c).Delete Shift:=xlToLeft
                        End If
                    End If
            End Select
        Next c
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thank you Joe...That's simply amazing!! It worked...
 
Upvote 0
Hello Joe,

I would require one last Macro if that's feasible. After deleting the blanks and the duplicate I have the below table now

data -

[TABLE="width: 256"]
<tbody>[TR]
[TD="width: 64, bgcolor: transparent"]A
[/TD]
[TD="width: 64, bgcolor: transparent"]B
[/TD]
[TD="width: 64, bgcolor: transparent"]C
[/TD]
[TD="width: 64, bgcolor: transparent"]D
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]B
[/TD]
[TD="bgcolor: transparent"]F
[/TD]
[TD="bgcolor: transparent"]G
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]H
[/TD]
[TD="bgcolor: transparent"]I
[/TD]
[TD="bgcolor: transparent"]D
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]K
[/TD]
[TD="bgcolor: transparent"]L
[/TD]
[TD="bgcolor: transparent"]M
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]N
[/TD]
[TD="bgcolor: transparent"]K
[/TD]
[TD="bgcolor: transparent"]P
[/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
</tbody>[/TABLE]

Output required is as below - macro has to check from each row if the same item has been repeated elsewhere in the data and bring the remaining data in the one row


[TABLE="width: 512"]
<tbody>[TR]
[TD="width: 64, bgcolor: transparent"]A
[/TD]
[TD="width: 64, bgcolor: transparent"]B
[/TD]
[TD="width: 64, bgcolor: transparent"]C
[/TD]
[TD="width: 64, bgcolor: transparent"]D
[/TD]
[TD="width: 64, bgcolor: transparent"]F
[/TD]
[TD="width: 64, bgcolor: transparent"]G
[/TD]
[TD="width: 64, bgcolor: transparent"]H
[/TD]
[TD="width: 64, bgcolor: transparent"]I
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]K
[/TD]
[TD="bgcolor: transparent"]L
[/TD]
[TD="bgcolor: transparent"]M
[/TD]
[TD="bgcolor: transparent"]N
[/TD]
[TD="bgcolor: transparent"]P
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
How about
Code:
Sub CopyDatatoOneRow()

   Dim Ary As Variant
   Dim Rw As Long
   Dim Col As Long
   
   Ary = Range("A1").CurrentRegion
   
   With CreateObject("scripting.dictionary")
      For Col = 1 To UBound(Ary, 2)
         For Rw = 1 To UBound(Ary, 1)
            If Not Ary(Rw, Col) = "" And Not .exists(Ary(Rw, Col)) Then .Add Ary(Rw, Col), Nothing
         Next Rw
      Next Col
      Sheets("[COLOR=#ff0000]Test1[/COLOR]").Range("A1").Resize(, .Count).Value = .keys
   End With
End Sub
This runs on the active sheet & will put the results on a sheet called Test1.
It assumes that you will never have more than 16,384 values
 
Upvote 0
Hello Joe,

I got an error message

Runtime Error 450 ( Wrong number of arguments or invalid property assignment.
 
Upvote 0
As you originally said that Joe4's code worked, I guessing that post#7 was aimed at me.
What line of code gave the error?
 
Upvote 0
Hello,


Sorry about that, I dint notice.


I re-run the code and it did run without any error but I did not get the desired output, will try to breakdown my requirement


1st row has 4 letters A,B,C,D.Code should search for each duplicate letter starting from the 1st row in the entire database and if any duplicates found then it should bring up the adjacent letters from that row to the 1st row.


A B C D
B F G
H I D
K L M
N K P


Once the 1st row search is complete from the above data the output should be


A B C D F G H I


Whereas for the below data these letters were not found in the 1st row, so the code should delete row 2 & row 3 and create new row2


K L M
N k P


Output should be


K L M N P


final output will be


A B C D F G H I
K L M N P
 
Upvote 0
If you had something like this


Excel 2013 32 bit
ABCD
1ABCD
2BFG
3HID
4KLF
5NKP
Data



Would you expect row 4 to be copied to row 1 as the F matches & then row 5 to also be copied as the K matches?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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