# Comparing two columns from excel

#### decci_7

##### New Member
General info on script functionality

It compares two column values from excel sheet and carries out a particular task after getting the desired results

Code:
``````Sub checkdata()

Range("A1").Select

Do Until Selection.Value = ""
v1 = Selection.Value
v2 = Selection.Offset(0, 1).Value
If v1 = v2 Then
Selection.Offset(0, 3).Value = "Y"
Else
Selection.Offset(0, 3).Value = "N"
End If
Selection.Offset(1, 0).Select
Loop
End Sub``````

### Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

#### GlennUK

##### Well-known Member
Is there a reason for this task not to be done via formulae? Looping one cell at a time seems very inefficient. I'd do a formula to calculate the Y/N, and then autofill down ( either by hand or by macro ).

#### decci_7

##### New Member
reasons

Yup, Thats true that it could have been achieved by using formulae but there were 2 reasons in specific for why i used the macro.

1. First, we are not sure upto how many rows we are going to do the calculations. It can be 10 rows, 100, 500, 5000, 50000 rows.

2. Second, This "Y/N" output is just a dummy one. There could have some other complex tasks or calculations after getting the desired output on comparisons.

I would like to make it clear that I'm still a novice at Excel macros and on a learning curve So the better ideas are always welcomed, if there are any, to achieve the required.

#### GlennUK

##### Well-known Member
I see, thanks. Well, the first advice I'd give would be to say that you don't have to select cells to be able to do work on them. Look at this:
Code:
``````Sub checkdata()

For Each c in Range("A1",Range("A1").End(xlDown))

If c.Value=c.Offset(0,1).Value Then
c.Offset(0, 3).Value = "Y"
Else
c.Offset(0, 3).Value = "N"
End If
Next
End Sub``````

#### decci_7

##### New Member
Thanks Glenn ! ..

If the number of records are on higher side and calculations are complex, ur code can save a lot of time as it doesn't waste time in selecting the cells.

Decci

Replies
4
Views
44
Replies
3
Views
92
Replies
0
Views
45
Replies
16
Views
123
Replies
5
Views
477