Compare Cell Value between 2 range of cells

vadius

Board Regular
Joined
Jul 5, 2011
Messages
70
Hello guys

I have two sheets : the first one contains all the source data, the second one contains some of those data.
I want to build a macro that compares the values of the first four cells in each row of sheet(1) with those of sheets (2) and if equal, then copies the entire row of sheets(2) into a sheet(3), else copies the entire row of sheets(1) into sheet(3).

The rationale behind is the following : Data from sheet(1) are updated automatically from an independant database, and in sheet(2) I change manually some of the values (But I keep the values of the first four cells of each row so that I compare). Sheet(3) combines both.

Below my macro : I don't know how to specify the range of cells to compare between sheet(1) and sheet(2) neither how to ask the macro compare those values. If equal then copy the entire row...

Below Worksheets("Index_Div_Source") is sheet(1) and Worksheets("Index_Div_Manual") is sheet(2) . Sourcecel are the first four cells of each row in the sheet(1) and manualcel the first four cell of each row in sheet(3). Worksheets("Index_Div_Final") is sheet(3) where I want to combine everything.

Hope it's clear,

Thanks you

Code:
Sub Compare()

Dim i As Integer
Dim manualcel As Range
Dim sourcecel As Range

Dim lastrow As Integer

Application.ScreenUpdating = False

lastrow = Worksheets("Index_Div_Source").Range("A65536").End(xlUp).Row


With Worksheets("Index_Div_Final")

For i = 3 To lastrow

Set manualcel = Worksheets("Index_Div_Manual").Range("A" & i, "D" & i)

For Each sourcecel In Worksheets("Index_Div_Source").Range("A" & i, "D" & i)

If sourcecel.Value = manualcel.Value Then

Worksheets("Index_Div_Manual").Range("A" & i).EntireRow.Copy
Worksheets("Index_Div_Final").Range("A" & i).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Else

Worksheets("Index_Div_Source").Range("A" & i).EntireRow.Copy
Worksheets("Index_Div_Final").Range("A" & i).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


End If

Next

Application.ScreenUpdating = True

Next

End With


End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
this is a hybrid solution

sample data in sheet no. 1 and sheet no.2 with the names of sheets are as follows ( see the formula in E2 in both sheets which are copied down)

Excel Workbook
ABCDE
1hdng1hdng2hdng3hdng4
2a29946a29946
3s43847s43847
4d475797d475797
5f988953f988953
6g81170g81170
7h37277h37277
Index_Div_Source


Excel Workbook
ABCDE
1hdng1hdng2hdng3hdng4
2a29946a29946
3d475797d475797
4g81170g81170
5k567k567
Index_Div_Manual


after fillilng formulas in column E in both sheets try this macro


Code:
Sub test()
Dim r As Range, c As Range, cfind As Range, x As String
With Worksheets("index_div_source")
Set r = Range(.Range("E2"), .Range("E2").End(xlDown))
For Each c In r
x = c.Value
With Worksheets("Index_Div_Manual")
Set cfind = .Cells.Find(what:=x, lookat:=xlWhole, LookIn:=xlValues)
If Not cfind Is Nothing Then
cfind.EntireRow.Copy
Else
GoTo nextc
End If
With Worksheets("sheet3")
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
End With
nextc:
Next c
End With

End Sub

Code:
Sub undo()
Worksheets("sheet3").Cells.Clear
End Sub
 
Upvote 0
Hi

Thanks for posting this.
I am actually looking for a way to compare the first four cells of each row from sheet(1) with the first four cells of the rows of sheet(2).
Somekind of vlookup but instead of looking for one value, I am looking at four values at one time . If found somewhere in the table, copy and paste the whole row into sheet(3). Otherwise, copy fron sheet(1) into sheet(3).

Thanks
 
Upvote 0
this is one way of solving what you want. instead of comparing each one of the four cells in a row you concatenate the four cells in the row in one cell (column E) and compare this column in both sheets. see the formula in E2 in both sheets which are copied down. study the solution carefully.
 
Upvote 0
It is actually not doing it. It effectively compares Index_Div_Source with Index_Div_Manual and copy into sheet3 when found. But I want it to copy the line of Source_Div when it does not find the values in Index_Div_Manual. Thanks
 
Upvote 0
I have modified marginally now check


Code:
Sub test()
Dim r As Range, c As Range, cfind As Range, x As String
With Worksheets("index_div_source")
Set r = Range(.Range("E2"), .Range("E2").End(xlDown))
For Each c In r
x = c.Value
With Worksheets("Index_Div_Manual")
Set cfind = .Cells.Find(what:=x, lookat:=xlWhole, LookIn:=xlValues)
If cfind Is Nothing Then
c.EntireRow.Copy
Else
GoTo nextc
End If
With Worksheets("sheet3")
.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial
End With
End With
nextc:
Next c
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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