Comparing two columns from excel

decci_7

New Member
Joined
Jul 17, 2006
Messages
22
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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

GlennUK

Well-known Member
Joined
Jul 8, 2002
Messages
11,547
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
Joined
Jul 17, 2006
Messages
22
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
Joined
Jul 8, 2002
Messages
11,547
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
Joined
Jul 17, 2006
Messages
22
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
 

Forum statistics

Threads
1,141,224
Messages
5,705,122
Members
421,378
Latest member
CarlosDuran

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
Top