VBA code needed to compare rows in excel 2010

genetist

Board Regular
Joined
Mar 29, 2013
Messages
75
Office Version
  1. 2016
Platform
  1. Windows
hi to all VBA programmers,
I have data like this with 6 columns</SPAN>
[TABLE="width: 384"]
<TBODY>[TR]
[TD]LINES</SPAN></SPAN>
[/TD]
[TD]AB1</SPAN></SPAN>
[/TD]
[TD]AB2</SPAN></SPAN>
[/TD]
[TD]AB3</SPAN></SPAN>
[/TD]
[TD]AB4</SPAN></SPAN>
[/TD]
[TD]AB5</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]P1</SPAN></SPAN>
[/TD]
[TD]Z/Z</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]-/-</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]P2</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]Z/Z</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]1</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]2</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]C/C</SPAN></SPAN>
[/TD]
[TD]C/C</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]3</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]4</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]C/C</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]5</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]P1</SPAN></SPAN>
[/TD]
[TD]Z/Z</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]-/-</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]P2</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]Z/Z</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]1</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]2</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]C/C</SPAN></SPAN>
[/TD]
[TD]C/C</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]3</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]G/G</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]4</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]C/C</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]5</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]T/T</SPAN></SPAN>
[/TD]
[TD]A/A</SPAN></SPAN>
[/TD]
[/TR]
</TBODY>[/TABLE]


  1. First I want to find how many columns (from AB1 to AB5) are different for P1 and P2 , </SPAN>
Eq means: Both P1 and P2 should contain same letters (alleles) or if any one of P1 or P2 contains Z/Z or -/- I should consider them as eq only. Now I am doing this using this formula =IF(D2=D3,"mono",IF(OR(D2="Z/Z",D2="-/-"),"mono",IF(OR(D3="Z/Z",D3="-/-"),"mono","poly")))</SPAN>


  1. I will compare lines column values from 1 with P2 across all the columns (from AB1 to AB5) in horizontal way and continue for remaining lines from 2 to 5. if they match I would like to give 1 else 0 and I would like to continue this till my programme encounters second set of P1 and P2. Presently I am doing this with this formula =if(D4=D$3,1,0).</SPAN>
  2. I will make sum for lines 1 to 5 across all the columns from columns AB1 to AB5, but I will include only columns showing different for P1 and P2 in my sum count. Now I am working on this with sumif formula.</SPAN>
  3. I will calculate percentage of matching lines 1 to 5 with P2 by dividing sum came from SUMIF with number of different markers between P1 and P2.</SPAN>
  4. I want to repeat this for remaining set of P1 and P2.</SPAN>
I am expecting like this</SPAN>
[TABLE="width: 512"]
<TBODY>[TR]
[TD]LINES</SPAN></SPAN>
[/TD]
[TD]XY1</SPAN></SPAN>
[/TD]
[TD]XY2</SPAN></SPAN>
[/TD]
[TD]XY3</SPAN></SPAN>
[/TD]
[TD]XY4</SPAN></SPAN>
[/TD]
[TD]XY5</SPAN></SPAN>
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]P1</SPAN></SPAN>
[/TD]
[TD]EQ</SPAN></SPAN>
[/TD]
[TD]NE</SPAN></SPAN>
[/TD]
[TD]EQ</SPAN></SPAN>
[/TD]
[TD]EQ</SPAN></SPAN>
[/TD]
[TD]EQ</SPAN></SPAN>
[/TD]
[TD]SUM</SPAN></SPAN>
[/TD]
[TD]%</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]P2</SPAN></SPAN>
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]2</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]100</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]3</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]4</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[/TR]
[TR]
[TD]5</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]0</SPAN></SPAN>
[/TD]
[TD]1</SPAN></SPAN>
[/TD]
[TD]100</SPAN></SPAN>
[/TD]
[/TR]
</TBODY>[/TABLE]

Like this I have data in more than 5000 rows and at present I am doing in excel 2010 with different formulas but it is taking lot of my energy. If anyone provide VBA code for this task I will be more happy and it would be appreciated.
</SPAN>
Regards,</SPAN>
Genetist.</SPAN>
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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