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:

Order IDProduct TypeDollar Amount
11Low grade20
11Premium35
11Low grade10
12Premium50
12Premium20
12Low grade10
12Low grade40
12Low grade45
12Premium30

<tbody>
</tbody>


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:


Order IDProduct TypeDollar Amount
11Low grade20
11Premium35
11Low grade10
12Premium50
12Premium40
12Low grade10
12Low grade20
12Low grade30
12Premium45

<tbody>
</tbody>


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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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