VBA code for compare two columns in excel, and copy the difference at the bottom of column 2

lilloscar

New Member
Joined
Nov 27, 2016
Messages
37
This code do exactly what I need but I can't figure it out how to adapt it to my needs...
Please can you tell me how can I change this to work instead searching from A1:C and coping missing data to D1:F to work only comparing two columns J1:J and K1:K

Attach: https://www.4shared.com/s/fI0i45Uxyda

Code:
Option Explicit

Sub test()
    Dim a, i As Long, ii As Long, x As Range
    With Range("a1").CurrentRegion
        a = .Value: ReDim b(1 To UBound(a, 1) * 2, 1 To 3)
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For i = 1 To UBound(a, 1)
                If a(i, 4) <> "" Then .Item(a(i, 4)) = Empty
            Next
            For i = 1 To UBound(a, 1)
                If Not .exists(a(i, 1)) Then
                    If x Is Nothing Then
                        Set x = Cells(i, 1).Resize(, 3)
                    Else
                        Set x = Union(x, Cells(i, 1).Resize(, 3))
                    End If
                End If
            Next
        End With
        If Not x Is Nothing Then
            x.Copy Cells(Rows.Count, 4).End(xlUp)(2)
            Set x = Nothing
        Else
            MsgBox "No new item"
        End If
    End With
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The next code do the same as the original but it can be configured to only two compared columns to extract his differences and copy them at the bottom of the second (results) column but without erasing nothing:

Code:
Option Explicit

Function LastRowInColumn(ByVal column As String) As Long
    With Application.ActiveSheet
        LastRowInColumn = .Cells(.Rows.Count, column).End(xlUp).Row
    End With
End Function

Sub test()
    ' Nombre de las columnas x y y
    Dim xCol As String, yCol As String
    ' Número de elementos en las columnas x y y
    Dim x, y, i As Long
    ' Rango de valores para las columnas x y y
    Dim xRng, yRng As Range
    ' Valor de la celda en el rango de columnas x y y
    Dim xCell, yCell As Range
    
    ' Definir columnas a utilizar
    xCol = "Q"
    yCol = "O"
    
    ' Obtener el número de elementos de dichas columnas
    x = LastRowInColumn(xCol)
    y = LastRowInColumn(yCol)
    
    ' Salvar en un rango los elementos de las columnas
    Set xRng = Worksheets(1).Range(xCol & 1 & ":" & xCol & x)
    Set yRng = Worksheets(1).Range(yCol & 1 & ":" & yCol & y)
    
    ' Colección para almacenar nuevos elementos a agregar en columna y
    Dim newItems As New Collection
    
    ' Iterar entre ambas columnas para ver cual valor es nuevo y debe ser agregado
    For Each xCell In xRng
        For Each yCell In yRng
            If StrComp(xCell.Text, yCell.Text) = 0 Then
                GoTo ContinueLoop
            End If
        Next
        
        newItems.Add (xCell.Text)
ContinueLoop:
    Next
    
    ' Agregar valores nuevos a columna y
    For i = 1 To newItems.Count
        Dim N As Long
        N = Cells(Rows.Count, yCol).End(xlUp).Row + 1
        Cells(N, yCol).Value = newItems.Item(i)
    Next i
End Sub

Sorry for the language, this is all the work and help from my coworker "@AlexandroSifuentes"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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