VBA - compare 2 columns and return matching values in another column

tikcr

New Member
Joined
Dec 20, 2022
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Hi All,
I am starting my journey with VBA, so sorry when this was already posted before, but I couldn't find satisfactionary response.
What I would like to do is to compare all values from Col 1 and Col 2 starting from the second row, and if the values match (if any of the value from Column 1 is in Column 2 or other way around, they should be displayed in 'Matching values'.

Column 1Column 2Matching values
1QWE2AsD
2asD2LLO
1ZXc1qwe
4ASZ1zXC

In this case, I would expect values 1QWE, 2ASD, 1ZXC to be displayed in Matching values column when I press the button 'Check' with assigned macro. I would highly appreciate your support!
 
VBA Code:
Option Explicit
Option Compare Text

Sub Foo()
    Dim i As Long, lr As Long, j As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        For j = 1 To lr
            If Range("A" & i) = Range("B" & j) Then
                Range("C" & i) = Range("B" & j)
            End If
        Next j
    Next i
    MsgBox "completed"
End Sub
 
Upvote 0
Thank you so much. It works, however when i delete the results of a check from 'Matching values', add some new values in col 1 and col 2 and try to run the script again, only msg box comes in but no results are then posted in the column 'Matching values'.
 
Upvote 0
Should work. Suppose you post your starting values and then show your new values in two different tables/ranges so we can test. Please load your data using XL2BB.
 
Upvote 0
Hello, thanks for your support. In the code I changed Range("C" & i) to Range("D" & i), so I am getting results in column D.
By the way, is it possible to have the result of common values cell by cell (so if both values are in Column 1 and Column 2 then paste the common value in D2, D3, D4, etc, without empty cells in-between?)

Then what I tried after the result is visible, I deleted all results from Matching Values and I am using new data (will be copied manually from another excel sheet) but as an example, I would like to paste now new data to Column 2 from 'Paste data...'.
After that I run the macro again and would like to get new 'Matching values' which come from comparison of Col 1 and Col 2. For some reason it does not work for me and only shows 1 common value between two columns which is wrong...

Here is the data I am using:


columns compare.xlsx
ABCDEFGH
1Column 1Column 2Matching valuesafter the check, press button 'Clear' and delete all results from 'Matching values'Paste new data into Column 2 and check matching values
2ZAU000016271ZAU000002917ZAU000014474
3ZAU000002891ZAU000017097ZAU000017097
4ZAU000002909ZAU000002917ZAU000012742
5ZAU000002917ZAU000012742ZAU000002891
6ZAU000002933ZAU000012718ZAU000012601
7ZAE400000010ZAU000008096ZAU000012353
8ZAU000008096ZAU000016271ZAU000013716
9ZAU000008187ZAU000013716ZAU000002891
10ZAU000008229ZAU000013724ZAU000012601
11ZAU000016792ZAU000012346ZAU000012346
12ZAU000012353ZAU000008096
13ZAU000012551ZAU000008096
14ZAU000012601ZAU000012353
15ZAU000014011ZAU000016271
16ZAU000014029
17ZAU000012965
18ZAU000012957
19ZAU000014474
20ZAU000017055
21ZAU000013872
22ZAU000008096
23ZAU000012353
24ZAU000016271
25ZAU000002891
Arkusz1
 
Upvote 0
Cannot replicate your issue. I delete the data in Columns A and D and then put new data in Column A. No issues.
Here is the new code to show no blank spaces

VBA Code:
Option Explicit
Option Compare Text

Sub Foo()
    Dim i As Long, lr As Long, j As Long, lr2 As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr
        For j = 1 To lr
        lr2 = Range("D" & Rows.Count).End(xlUp).Row
            If Range("A" & i) = Range("B" & j) Then
                Range("D" & lr2 + 1) = Range("B" & j)
            End If
        Next j
    Next i
    MsgBox "completed"
End Sub
 
Upvote 0
Another way if I understand your requirement right.
Code:
Sub Maybe()
Dim lr1 As Long, lr2 As Long, i As Long
lr1 = Cells(Rows.Count, 1).End(xlUp).Row
lr2 = Cells(Rows.Count, 2).End(xlUp).Row
    For i = 2 To lr2
        If WorksheetFunction.CountIf(Range("A2:A" & lr1), Range("B" & i)) <> 0 Then Cells(Rows.Count, 4).End(xlUp).Offset(1).Value = Range("B" & i).Value
    Next i
End Sub
Here I assumed Column B to be the shortest data Column. Change the references so you'll loop through the shortest column if different setup.
 
Upvote 0

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