VBA - apply formula to a range of cells if adjacent cell value meets the criteria

hrayani

Well-known Member
Joined
Jul 23, 2010
Messages
1,501
Office Version
  1. 2016
Platform
  1. Windows
Hello Friends,

I need a VBA code to apply these formula in Columns C:F if Column M is showing > In Process

Formula for Column C
Excel Formula:
=H2-SUMIF($B$1:$B1,$B2,C$1:C1)

Formula for Column D
Excel Formula:
=I2-SUMIF($B$1:$B1,$B2,D$1:D1)

Formula for Column E
Excel Formula:
=J2-SUMIF($B$1:$B1,$B2,E$1:E1)

Formula for Column F
Excel Formula:
=K2-SUMIF($B$1:$B1,$B2,F$1:F1)

Pls Note:
1) Column M Values are formula result
2) Leave the values as it is where there condition is not met. Like where Column M does not equal to In Process then leave the values in Column C:F as it is

Any help would be appreciated

Book1
ABCDEFGHIJKLM
1DATEPO #CuttingStitchingCheckingPackingCuttingStitchingCheckingPackingStatus
201-Jul534100755025100755025Shipped
303-Jul52512510075501251007550In Process
405-Jul5345050505015012510075In Process
507-Jul52550505050175150125100In Process
609-Jul53450505050200175150125Shipped
711-Jul53425252525225200175150Shipped
Sheet1


Regards,
Humayun


EDIT:
I just realized that formulas will be applied after running the code. But what when the In Process order is shipped
So, at the end to the code if it's possible to look at the range & if any row containing formulas are shipped in Column M then it should convert those formulas to values
 
Last edited:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
IMHO this is not a good way to work with data.
I would think you can use a pivot table (or many other ways) to summarize your data and get a proper report if your structure it properly. But messing around with it like this is not OK.
Or turn everything into smart formulas so you don't have to repeat such a task.
That's just my opinion ...
 
Upvote 0
Hi bobsan,

Thanks for the reply & suggestion.
The actual data is some 15,000 rows long and keeps on growing & the reason behind all this is to save some time as such a huge data takes long time to calculate.
After some searching on the internet & dozens of attempt I got it working with only one problem left i.e. the formula does not pick relative reference.
I have prepared a code for single column (C).

How to amend it to make it work in a way that it picks up relative reference

VBA Code:
Sub humayun()

Dim rng As Range
 
For Each rng In Range("M2:M7")
    If rng.Value = "In Process" Then rng.Offset(0, -10).Formula = "=H2-SUMIF($B$1:$B1,$B2,C$1:C1)"
    If rng.Value = "Shipped" Then rng.Offset(0, -10).Value = rng.Offset(0, -10).Value
    Next rng
 
End Sub
 
Upvote 0
Solution
I got it working by changing the formula to R1C1 style

VBA Code:
If rng.Value = "In Process" Then rng.Offset(0, -10).Formula = "=RC[5]-SUMIF(R1C2:R[-1]C2,RC2,R1C:R[-1]C)"

Now it is picking the relative reference
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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