How to copy adjacent cell

ceclay

Board Regular
Joined
Dec 4, 2019
Messages
58
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have this code below which find unique words on 2 columns.

Code:
Sub Match1()
    
    Dim CC As Worksheet
    Dim lrA As Long
    Dim lrE As Long
    Dim rngA As Range
    Dim rngE As Range
    Dim cell As Range
    
    Application.ScreenUpdating = False
    
    Set CC = Sheets("Sheet2")
    
'   Find last row with data in column A
    lrA = CC.Cells(Rows.Count, "A").End(xlUp).Row
'   Find last row with data in column B
    lrE = CC.Cells(Rows.Count, "E").End(xlUp).Row
    
'   Set data ranges
    Set rngA = CC.Range("A2:A" & lrA)
    Set rngE = CC.Range("E2:E" & lrE)
    
'   Loop through all rows in column E
    For Each cell In rngA
'       Search for value in column A
        If Application.WorksheetFunction.CountIf(rngE, cell.Value) = 0 Then
'           Copy value to column F
            Cells(Rows.Count, "F").End(xlUp).Offset(1, 0) = cell.Value
        End If
    Next cell

    
    Application.ScreenUpdating = True
    
    
End Sub

My problem now is how I am going to copy its to make it like below: (Note that from code above I was only able to copy the unique words on Column A and not its adjacent cell)
Apple1234AppleBanana1345
Banana1345GrapesGuyabano4566
Guyabano4566SantolPear543
Grapes211CherryMelon211
Pear543PomegrateOranges32
Melon211Watermelon
Apple1452
Santol12
Watermelon23
Oranges32
Cherry55


Hoping for your response.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi. Try after adding the red line as below.
Cells(Rows.Count, "F").End(xlUp).Offset(1, 0) = cell.Value
Cells(Rows.Count, "G").End(xlUp).Offset(1, 0) = cell.Offset(, 1).Value


just a note abaout this comment ~~~> ' Loop through all rows in column E ~~~> the looping is running on column A instead

As an alternative, if Sheets2 is the active sheet as you run the code, then the simplified code below will do the job too.
VBA Code:
Sub Match1V2()
 Dim cell As Range
  Application.ScreenUpdating = False
  For Each cell In Range("A2:A" & Cells(Rows.Count, 1).End(3).Row)
   If Application.CountIf([E:E], cell.Value) = 0 Then
    Cells(Rows.Count, "F").End(3)(2) = cell.Value
    Cells(Rows.Count, "G").End(3)(2) = cell.Offset(, 1).Value
   End If
  Next cell
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,528
Messages
6,125,342
Members
449,218
Latest member
Excel Master

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