Compare column A with several columns and copy the non-existent one

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello, I have this code to fetch the values in column A and not in column C and copy them after the last cell that contains a value from column C. I want to execute the same command on several columns, so that column A is compared to columns C through R, and the non-existent ones are copied in each column separately.

VBA Code:
Sub test()

Dim lr As Long, i As Long
Dim WS As Worksheet: Set WS = Worksheets("data")
 Application.ScreenUpdating = False
lr = WS.Columns("A:R").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    For i = 5 To lr
  

    If WorksheetFunction.CountIf(Range("C5:C" & lr), Range("A" & i)) = 0 Then
    Cells(Rows.Count, 3).End(xlUp).Offset(1).Value = Range("a" & i).Value
     End If
    
    Next i
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi sofas,

Try this:

VBA Code:
Option Explicit
Sub test()

    Dim lr As Long, i As Long, j As Long
    Dim strCol As String
    Dim WS As Worksheet: Set WS = Worksheets("data")
    
    Application.ScreenUpdating = False
    
    lr = WS.Columns("A:R").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
    
    For i = 3 To 18 'Columns C to R
        strCol = Split((WS.Columns(i).Address(, 0)), ":")(0) 'Though not necessary I find using the actual column letter as opposed to its number much easier to follow
        For j = 5 To lr
            If WorksheetFunction.CountIf(WS.Range(strCol & "5:" & strCol & lr), WS.Range("A" & j)) = 0 Then
                WS.Cells(Rows.Count, strCol).End(xlUp).Offset(1).Value = WS.Range("A" & j).Value
            End If
        Next j
    Next i
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution
Hi sofas,

Try this:

VBA Code:
Option Explicit
Sub test()

    Dim lr As Long, i As Long, j As Long
    Dim strCol As String
    Dim WS As Worksheet: Set WS = Worksheets("data")
   
    Application.ScreenUpdating = False
   
    lr = WS.Columns("A:R").Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
   
    For i = 3 To 18 'Columns C to R
        strCol = Split((WS.Columns(i).Address(, 0)), ":")(0) 'Though not necessary I find using the actual column letter as opposed to its number much easier to follow
        For j = 5 To lr
            If WorksheetFunction.CountIf(WS.Range(strCol & "5:" & strCol & lr), WS.Range("A" & j)) = 0 Then
                WS.Cells(Rows.Count, strCol).End(xlUp).Offset(1).Value = WS.Range("A" & j).Value
            End If
        Next j
    Next i
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
Thank you, this is what is really needed
. amazing.........
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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