so I have two worksheets with data, I want to compare column A of worksheet 1 to column A of worksheet 2 . Worksheet 1 is a template and worksheet 2 is a data report. Column A for both worksheets are item #s, worksheet 1 updates item #s and data from the report in worksheet 2. I want to create a formula or I guess vba code (I know nothing about VBA) whatever I can that will compare the item#s. I want the formula/code to pull the data from the report in worksheet 2 to the corresponding item #s in the template of worksheet 1 and high light any new item #s in the report(column A) of worksheet 2 that are not in worksheet 1 and then add those new high lighted item #s and its data to worksheet 1.
The range of data in column A of worksheet 1 is less than the range of data in column A of worksheet 2 that im trying to compare and pull from.
I thought about creating a IF formula in worksheet 2 to show me which item number exist, which are new but because the range of data in column A of worksheet 1 is less than column A of worksheet 2 I end getting an error value, I think creating a vba may be better but im not sure . Any advice, please give step by step detail/image of what to do. So far in general for the template I have a vlookup to pull the data BUT I need to know which items are new and highlight/pull them. Also if I could either delete or label the items numbers that is discontinued as discontinued that would be great. I think so far when I do a vlookup the items that are in worksheet 1 and not worksheet 2 come up as #N/A are the discontinued items. I have excel 2007, I hope you can help.
Sample Sheet1 and Sheet2
Output after updating using Vlookup
I am trying to compare three different columns of data in three worksheets and highlight the differences using VBA. I am very new to VBA and I don't know a lot about programming. So far this is what I've done:
worksheet1
:
Sub compare_cols()
Dim myRng As Range
Dim lastCell As Long
'Get the last row
Dim lastRow As Integer
lastRow = ActiveSheet.UsedRange.Rows.Count
'Debug.Print "Last Row is " & lastRow
Dim c As Range
Dim d As Range
Application.ScreenUpdating = False
For Each c In Worksheets("worksheet1").Range("A2:A" & lastRow).Cells
For Each d In Worksheets("worksheet2").Range("A2:A" & lastRow).Cells
c.Interior.Color = vbGreen
If (InStr(1, d, c, 1) > 0) Then
c.Interior.Color = vbWhite
Exit For
End If
Next
Next
For Each c In Worksheets("worksheet2").Range("A2:A" & lastRow).Cells
For Each d In Worksheets("worksheet1").Range("A2:A" & lastRow).Cells
c.Interior.Color = vbYellow
If (InStr(1, d, c, 1) > 0) Then
c.Interior.Color = vbWhite
Exit For
End If
Next
Next
Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
Worksheet 2:
Sub compare_cols()
Dim myRng As Range
Dim lastCell As Long
'Get the last row
Dim lastRow As Integer
lastRow = ActiveSheet.UsedRange.Rows.Count
'Debug.Print "Last Row is " & lastRow
Dim c As Range
Dim d As Range
Application.ScreenUpdating = False
For Each c In Worksheets("worksheet2").Range("A2:A" & lastRow).Cells
For Each d In Worksheets("worksheet3").Range("A2:A" & lastRow).Cells
c.Font.Color = rgbRed
If (InStr(1, d, c, 1) > 0) Then
c.Font.Color = rgbBlack
Exit For
End If
Next
Next
For Each c In Worksheets("worksheet3").Range("A2:A" & lastRow).Cells
For Each d In Worksheets("worksheet2").Range("A2:A" & lastRow).Cells
c.Interior.Color = vbRed
c.Font.Color = rgbWhite
If (InStr(1, d, c, 1) > 0) Then
c.Interior.Color = vbWhite
c.Font.Color = rgbBlack
Exit For
End If
Next
Next
Application.ScreenUpdating = True
End Sub
.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
Problems I'm having:
- in comparing
worksheet1
andworksheet2
, data that is not inworksheet2
are highlighted green inworksheet1
, and data that is not inworksheet1
are highlighted yellow inworksheet2
. - some of the data in
worksheet2
for example are highlighted in yellow but are found inworksheet1
, which shouldn’t happen. Then in comparingworksheet2
andworksheet3
, items that are not inworksheet3
have a red colored font, inworksheet2
, and items that are not inworksheet2
are highlighted red with a white font, inworksheet3
. - data in
worksheet2
for example have a red colored font but are found inworksheet3
, which shouldn’t happen.
Can you please tell me why my VBA code isn't working, or what I else I can do?
Would conditional formatting work for your needs? Here's a way to highlight cells that appear in another range (or don't appear, if you throw a "not(" in the mix).
http://www.techrepublic.com/blog/microsoft-office/use-conditional-formatting-to-compare-lists-in-excel/
I hope it helps!
JSR
If you want to do this automatically - it's about using VBA. But you don't have any knowledge of it, so I will not give you ready VBA solution.
To achieve your goal you can use
Remove duplicates
. With this you will create list of unique items - this will be list of all items from workbook1 and workbook2. Later you can create column withvlookup
function as you did before. If item was found you will get Qty as you wanted, if not you will get#N/D
which you can highlight using conditional formatting.I guess it should solve your problem without VBA.