• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Compare column A with column D and retain only A values found

Niranjan1979

New Member
Hi Team,

I need you help in VBA macro to find column value in Column D and retain only A values.

Please see xl attached.

Thank you
nin
 

Attachments

  • Extract.xlsx
    8.9 KB · Views: 8
@Niranjan1979
Something like this ...
My results are in F-column after You press [Do it].
It's a little different than Your 'Output Should be'!
Is there any clear rules like: 'i' = 'o' or 'i' = 'rr'?
 

Attachments

  • Extract.xlsb
    19.6 KB · Views: 2
Great, Thank you. This is fine. One quick question, if no matching is found that relative row has to be empty, but now it is shifting the bottom cell..please see the file attached.
 

Attachments

  • Extract123.xlsm
    20 KB · Views: 4
@Niranjan1979
Do You mean 'double' 'Morris\Jerry\Ruth' or that cell F2 should be empty?
You original sample has option that 'result' will write from row 1 to below, no connection of original row.
Like if D1 has only 'Raghu' then 'Morris\Jerry\Ruth' will write in cell F1.
Of course, this is possible to change other way too.
Clear rules makes easier to do things
... sometimes.
 
@Niranjan1979
I hope that You didn't mean that ALWAYS cell F2 have to be empty!
I hope that You meant that if any D-column text will found from A-column then copy that D-column text same row to F-column. If there (F-column and row) are more than one text then add "\" between those texts....
 

Attachments

  • Extract123_latest.xlsm
    20.7 KB · Views: 4
Hi !

A bite late but easier way even on a Mac (vletm ;)) :​
Code:
Sub Demo1()
    With Sheet1
        .UsedRange.Columns(6).ClearContents
        VA = .Cells(1).CurrentRegion.Columns(1).Value
        VD = .Range("D1", .Cells(.Rows.Count, 4).End(xlUp)).Value
    For R& = 1 To UBound(VD)
            SP = Split(VD(R, 1), "\")
        For N& = 0 To UBound(SP)
            If IsError(Application.Match(SP(N), VA, 0)) Then SP(N) = False
        Next
            VD(R, 1) = Join(Filter(SP, False, False), "\")
    Next
        .Cells(6).Resize(UBound(VD)).Value = VD
    End With
End Sub
Do you like it ? So thanks to click on bottom right Like !

In case of thousands data to check, faster ways but only on PC
(sorry vletm :() using a Dictionary object or RegEx
 
When I moved my data down, unwanted slashes are coming, any help on this, tired to use countA to calculate and tried to move cursor down no luck...Marc code also doesn;t work here...
 

Attachments

  • Extract123_latest123.xlsm
    20.7 KB · Views: 3
Little update for the new layout :​
Code:
Sub Demo1a()
         Const FR = 4
    With Sheet1
             Range(.Cells(FR - 1, 6), .Cells(.Rows.Count, 6).End(xlUp)).Offset(1).Clear
        VA = Range(.Cells(FR, 1), .Cells(FR - 1, 1).End(xlDown)).Value
        VD = Range(.Cells(FR, 4), .Cells(.Rows.Count, 4).End(xlUp)).Value
    For R& = 1 To UBound(VD)
            SP = Split(VD(R, 1), "\")
        For N& = 0 To UBound(SP)
            If IsError(Application.Match(SP(N), VA, 0)) Then SP(N) = False
        Next
            VD(R, 1) = Join(Filter(SP, False, False), "\")
    Next
        .Cells(FR, 6).Resize(UBound(VD)).Value = VD
    End With
End Sub
You like ? So thanks to …
 
Totally different method.
Code:
Option Explicit

Sub test()
    Dim a, i As Long, myPtn As String, m, temp As String
    myPtn = Join(Filter([transpose(if(a1:a1000<>"",a1:a1000,char(2)))], Chr(2), 0), Chr(2))
    With Range("d1", Range("d" & Rows.Count).End(xlUp))
        a = .Value
        With CreateObject("VBScript.RegExp")
            .Global = True: .IgnoreCase = True
            .Pattern = "([$()\-^|\\{}\[\]*+?.])"
            myPtn = Replace(.Replace(myPtn, "\$1"), Chr(2), "|")
            .Pattern = "\b(" & myPtn & ")\b"
            For i = 2 To UBound(a, 1)
                For Each m In .Execute(a(i, 1))
                    temp = temp & "\" & m.Value
                Next
                a(i, 1) = ""
                If Len(temp) Then a(i, 1) = Mid$(temp, 2): temp = ""
            Next
        End With
        .Offset(, 2).Value = a
    End With
End Sub
 

Attachments

  • Extract123_latest123 with code.xlsm
    20.3 KB · Views: 6
Need one more to remove duplicates using VBA code, Formula works, but code making confusion with array...please help
 

Attachments

  • REmove duplicate.xlsx
    9.4 KB · Views: 4

If you ever read Cells VBA inner help,
no confusion with Cells(#Row, #Column) !

FR variable within my code is relative to first row of data (under headers) …
 
Hello Mr. MarcL
How can I get VBA inner help ?
I tried pressing F1 while in VBA editor but I a new tab in my browser opens at msdn ..
Is this the inner help you mean?
 
Hi !

It depends from Excel version but if VBA inner help is installed,
just desactivate the Web help, must be an icon at bottom
or within Excel options …
But you can have the same information within MSDN : help is help !
 
Marc L,

Could you please help me to get a VBA code to remove duplicates in my Excel file...
 

Attachments

  • REmove duplicate.xlsx
    9.4 KB · Views: 0
In Cell A:

Sam\Ruth\Morrison\Ruth\Sam

In B column the output has to be: Sam\Ruth\Morrison

I many have n number of rows in Cell A like that, B should give result excluding duplicates
 

Attachments

  • REmove duplicate.xlsx
    9.4 KB · Views: 4
Thank you. One more quick help, How to find first cell that has no fill in a range.. for example please see excel file...range (D12:I12)--in this case the result is D4
 

Attachments

  • Empty.xlsx
    8.3 KB · Views: 2
Back
Top