VBA to swap values

sonic2000

New Member
Joined
Apr 7, 2018
Messages
5
Hi, I have a large dataset, about 4000 rows, which I need to correct some values in case of a criteria. Below is the example of my data:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Order ID[/TD]
[TD]Product Type[/TD]
[TD]Dollar Amount[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Low grade[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Premium[/TD]
[TD]35[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Low grade[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Premium[/TD]
[TD]50[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Premium[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Low grade[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Low grade[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Low grade[/TD]
[TD]45[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Premium[/TD]
[TD]30[/TD]
[/TR]
</tbody>[/TABLE]


In each of distinct order IDs, the dollar amount of premium products must be higher than low grade ones. As seen in my data, for order ID "11", there is no issue since premium one is larger than low grade ones: 35>20>10.

But in order ID "12", as it is seen, there are two low grade orders which have higher dollar values than two of the premium ones. To fix this issue, I must swap the value of the low grade ones (40,45) with the premium ones (20,30). So, after correction, the data should look like the following:


[TABLE="width: 500"]
<tbody>[TR]
[TD]Order ID[/TD]
[TD]Product Type[/TD]
[TD]Dollar Amount[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Low grade[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Premium[/TD]
[TD]35[/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Low grade[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Premium[/TD]
[TD]50[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Premium[/TD]
[TD]40[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Low grade[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Low grade[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Low grade[/TD]
[TD]30[/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]Premium[/TD]
[TD]45[/TD]
[/TR]
</tbody>[/TABLE]


I have similar issues with many of the order IDs in my dataset which I need a VBA code to go through all the order IDs and fix this issue if exists.
 
You could put this in row 2 and drag down to get the changed product types:
=IF(COUNTIFS($A:$A,$A2,$C:$C,">="&$C2)<=COUNTIFS($A:$A,$A2,$B:$B,"Premium*"),"Premium ","Low Grade ")&SUBSTITUTE(SUBSTITUTE(B2,"Low grade",""),"Premium","")
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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