Excel macro to change the values of a sheet based on 2 different sheets

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
205
Office Version
  1. 2019
Platform
  1. Windows
I have 3 sheets in excel, Sheet1, Shee2 and Base price


In sheet1 I have values in Column A and Column B, Column A is the Product Code and Column B is the values, In column A there are multiple product codes and multiple values are assigned to it.


In Sheet 2 also I have values in Column A and Column B, Column A is the product Code and B is the values. and in this sheet in column B the sum of negative is always more than the sum of positive.


In Base price I have values in Column A and Column B, Column A is the product code and Column B is the values against the product code.

shee%2B1.JPG
shee%2B2.JPG
base%2Bprice.JPG


Example Sheet 1 Sheet 2 and Base price sheet


3
result.JPG


The result should look like this



I want a macro that the sum of positive here its 40 is to be deducted from sheet1 as compare to the values in sheet3.


So in the above example I need to deduct 40 only. I can deduct the value 40 from the product codes of 2,3,6 only as in sheet2 these 3 product codes are having negative values.


Hence it will first check the maximum negative value in Sheet2 that's -20 in B2 hence it will check Base Price sheet whats the Base price of product code 2, its 10. then it will check the sum of values product code in Sheet1, its 24(B2+B3) now I want the maximum deduction it can made by keeping the base price. As base price is 10 it will keep that and the rest amount 14 it need to deduct from sheet1 under product code 2 category. hence the code should make B2 as 0 and B3 as 10.

then it will deduct the rest amount that is 40-14 = 26 it will select the next maximum value in sheet2 that is -15 under product code 3, base price of product code 3 under base price sheet is 10, then it will check the sum of values product code in Sheet1 its 25(B4+B5+B6). It will deduct 15 maximum so B4 should be 0 and B5 should be 0 and B6 should have value 10 as base price is 10.


till now it deducted 29 from sheet1 (14 from Product code 2 and 15 from product code 3) now the balance value is 40-29 = 11. base price of value 6 is 80 and sum of values product code 6 is 100 in sheet1 and I need to deduct 11 from that hence B10 should be 89.


What should be the macro code for that ?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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