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.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Like this ?
Excel Workbook
ABC
1Order IDProduct TypeDollar Amount
211Low grade10
311Low grade20
411Premium35
512Low grade10
612Low grade20
712Low grade30
812Premium40
912Premium45
1012Premium50
Sheet1


That was done using SORT three seqential filters
Order ID > Values > Smallest to Largest
Dollar Amount > Values > Smallest to Largest
Product Type . Values > Z to A
 
Upvote 0
Welcome to the MrExcel board!

1. Would it matter if, for example, the first 3 rows became as below? That is, cells C2 and c4 have swapped values?
Excel Workbook
ABC
1Order IDProduct TypeDollar Amount
211Low grade10
311Premium35
411Low grade20
Swap Values (eg)



2. Would it matter if the product Type values were rearranged? For example, like below?

Excel Workbook
ABC
1Order IDProduct TypeDollar Amount
211Low grade10
311Low grade20
411Premium35
Swap Values (eg)



3. Do you really need a macro? Depending on your answer to 1 above, would a formula in the next column to show the new values be okay?

Excel Workbook
ABCD
1Order IDProduct TypeDollar Amount
211Low grade2010
311Premium3535
411Low grade1020
512Premium5050
612Premium2045
712Low grade1010
812Low grade4020
912Low grade4530
1012Premium3040
Swap Values (eg)
 
Upvote 0
Peter,

Thanks a lot. The formula works perfect. However, it would be great if I can have it in VBA. Because I get such a data every week (~4000 rows) which I need to first fix this issue and then I have a VBA code to run and generate a report out of it.

So I want to add a code at the beginning of my macro to first fix the issue.
 
Upvote 0
Peter,

Thanks a lot. The formula works perfect.
That indicates that the answer to my Q1 is that it doesn't matter about the order in the third column as the formula swaps the 10 and 20 in the first 3 rows of the sample data.

What is the answer to my Q2 above?
 
Upvote 0
Peter,

The order is not important. It is also both possible to swap the values or swap the types.

However, I also noticed an issue with the formula you provided. Below is the sample of the real data I have. There are five different types: premium, lower grade, classic, EIP, and commissionable, which I want to fix that issue. (in case of existing other types than Premium within the same order number, the dollar amount of those shouldn't be higher than Premium. In case there are any, the value must be swapped with the premium)


111
Premium Phones

<tbody>
</tbody>
50
111
Premium Phones

<tbody>
</tbody>
25
111
Premium Phones

<tbody>
</tbody>
10
111
Commissionable SIM

<tbody>
</tbody>
50
222
Premium Phones

<tbody>
</tbody>
22
222
Lower Grade Device

<tbody>
</tbody>
50
222
Lower Grade Device

<tbody>
</tbody>
50
333
Premium Phones

<tbody>
</tbody>
22
333
Commissionable SIM

<tbody>
</tbody>
50
333
Commissionable SIM

<tbody>
</tbody>
50
333
Commissionable SIM

<tbody>
</tbody>
10
444
Premium Phones

<tbody>
</tbody>
25
444
Premium Phones

<tbody>
</tbody>
25
444
Lower Grade Device

<tbody>
</tbody>
25
444
Lower Grade Device

<tbody>
</tbody>
50
444
Lower Grade Device

<tbody>
</tbody>
50
444Commissionable SIM10
444Commissionable SIM10
444
Commissionable SIM

<tbody>
</tbody>
10

<tbody>
</tbody>

*The formula doesn't work properly for the order number "444" since there are two other types, lower grade and commissionable than premium.
 
Upvote 0
So, does it matter what values all the grades other than 'Premium' get, or is the only important thing that the 'Premium' get the values?

If it does matter what the others get, could you please provide the expected results for that sample data.
 
Upvote 0
The point is that there shouldn't be any order other than premium that has higher dollar amount than the premium. So if there is any, the dollar amount should be swapped.
And the arrangement is not important.

IDTypeAmountAdjusted
111
Premium Phones

<tbody>
</tbody>
5050
111
Premium Phones

<tbody>
</tbody>
2525
111
Premium Phones

<tbody>
</tbody>
1050
111
Commissionable SIM

<tbody>
</tbody>
5010
222
Premium Phones

<tbody>
</tbody>
2250
222
Lower Grade Device

<tbody>
</tbody>
5022
222
Lower Grade Device

<tbody>
</tbody>
5050
333
Premium Phones

<tbody>
</tbody>
2250
333
Commissionable SIM

<tbody>
</tbody>
5022
333
Commissionable SIM

<tbody>
</tbody>
5050
333
Commissionable SIM

<tbody>
</tbody>
1010
444
Premium Phones

<tbody>
</tbody>
2550
444
Premium Phones

<tbody>
</tbody>
2550
444
Lower Grade Device

<tbody>
</tbody>
2525
444
Lower Grade Device

<tbody>
</tbody>
5025
444
Lower Grade Device

<tbody>
</tbody>
5025
444
Commissionable SIM

<tbody>
</tbody>
1010
444
Commissionable SIM

<tbody>
</tbody>
1010
444
Commissionable SIM

<tbody>
</tbody>
1010

<tbody>
</tbody>
 
Upvote 0
Would this result be acceptable?
We'll try to get the result correct before worrying about the vba.

Excel Workbook
ABCD
1IDTypeAmountAdjusted
2111Premium Phones5050
3111Premium Phones2550
4111Premium Phones1025
5111Commissionable SIM5010
6222Premium Phones2250
7222Lower Grade Device5022
8222Lower Grade Device5050
9333Premium Phones2250
10333Commissionable SIM5010
11333Commissionable SIM5022
12333Commissionable SIM1050
13444Premium Phones2550
14444Premium Phones2550
15444Lower Grade Device2510
16444Lower Grade Device5010
17444Lower Grade Device5010
18444Commissionable SIM1025
19444Commissionable SIM1025
20444Commissionable SIM1025
Swap 2
 
Upvote 0
Thanks Peter. It’s fine if there is no better way. Because it fixes the premium values issue but it also changes the values of commissionable sim as well (order ID 444) which I prefer it not to.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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