Compare 2 columns on 2 different worksheets and copy to 2nd worksheet if cell does not meet criteria

nzamparo

New Member
Joined
Jun 2, 2011
Messages
3
Hi all!

I'm hoping that someone can provide some direction on a code I'm currently working on.

I have a workbook with multiple worksheets, and would like to compare Column "L" on the "TransitionGrid-Dish" worksheet to Column A in the "LikeProfiles" worksheet. If the Column L values on the first worksheet are not contained in Column A, I would like to be able to copy them to the next blank row in Column A on the second worksheet.

I've come up with the following code so far:

Code:
Sub CompareandHardcode()

Dim ws As Worksheet
    Dim wb As Workbook
     
    Set wb = ActiveWorkbook
    Set ws = wb.Worksheets(1)

i = 1
Do
If Sheets("TRANSITIONGRID-DISH").Cells(i, 12).Value = "" Then
        Exit Do
    End If
    Set valFound = Sheets("LIKEPROFILES").Columns("A").Find(Sheets("TRANSITIONGRID-DISH").Cells(i, 12).Value, , , xlWhole)
    If valFound Is Nothing Then
        lastrow = Sheets("LIKEPROFILES").UsedRange.Rows.Count
        Sheets("LIKEPROFILES").Range("A" & lastrow & ":A" & lastrow) = Sheets("TRANSITIONGRID-DISH").Range("L" & i & ":L" & i)
    End If
    i = i + 1
Loop
    With ws
         
    End With
End Sub

Can anyone please tell me if I'm on the right track?! I feel so close, but the code as written above doesn't execute any action. I would really, really appreciate any help anyone would be kind enough to offer!

Nikki
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
nzamparo,

It would have been better if you had supplied screenshots of all three worksheets.

The test workbook contained worksheets TransitionGrid-Dish, LikeProfiles, and second.

Sample worksheets:


Excel 2007
L
1123
2234
3345
4456
5467
6100
7200
8678
9300
10789
11400
12
TransitionGrid-Dish



Excel 2007
A
1Title A
2123
3234
4456
5467
6200
7678
8300
9400
10
LikeProfiles



Excel 2007
A
1Title A
2
3
4
5
second


After the macro:


Excel 2007
A
1Title A
2345
3100
4789
5
second


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub CompareandHardcodeV2()
' hiker95, 03/23/2013
' http://www.mrexcel.com/forum/excel-questions/693174-compare-2-columns-2-different-worksheets-copy-2nd-worksheet-if-cell-does-not-meet-criteria.html
Dim i As Long, lr As Long, fr As Long, nr As Long
With Sheets("TransitionGrid-Dish")
  lr = .Cells(Rows.Count, 12).End(xlUp).Row
  For i = 1 To lr Step 1
    fr = 0
    On Error Resume Next
    fr = Application.Match(.Cells(i, 12), Sheets("LikeProfiles").Columns(1), 0)
    On Error GoTo 0
    If fr = 0 Then
      nr = Sheets("second").Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      Sheets("second").Cells(nr, 1).Value = .Cells(i, 12).Value
    End If
  Next i
End With
Sheets("second").Activate
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CompareandHardcodeV2 macro.
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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