Vba for making a calculation based on condition

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
533
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
Hi,

we have a large data where we want to get some result, we have formula but want to convert it to in vba

Our Steps are
1. First time we are checking the value in T column if we have got the value then second step
2. We have got the value in T8, now we have put the formula
Rich (BB code):
=IF(G13="","",IF(G13>=T8,G13-T8,""))
in O13 and in P13 =IF(G13="","",IF(G13>=U8,G13-U8,""))
3. We want to repeat the task, we are changing the value of T column in every Friday, you can check the formula which we have used, but we have used it manually and now want to do it through vba

Note if there is no value in D column then it will not add formula

PF the demo data
vba database.xlsx
ABCDEFGHIJKLMNOPQRSTU
1Head1Head2Head3Head4Head5Head6Head7Head8Head9Head10Head11Head12Head13Head14Head15Head16Head17Head18
2MondayGasE02-Nov-20220.5221.4226.6216.1219.95219.4220.42446117242232
3TuesdayGasE03-Nov-20219.35221228.45220.05223.5223223.36341433931141
4WednesdayGasE04-Nov-20222.95226.95233212230.05231.4225.74791909668506
5ThursdayGasE05-Nov-20231.35234236.65227.6235.5235.5233.41242558124493
6
7FridayGasE06-Nov-20235.45235.1242.7233.05239.05239.9238.59241739729433
8MondayGasE09-Nov-20239.85243247.3237238.3238.8241.75214591327056opening stock247.3248
9TuesdayGasE10-Nov-20238.8240240.4231.25237236.7235.04252056420746Closing stock231.25231
10WednesdayGasE11-Nov-20236.7234.1238.7231.4233.2234.2234.68117227019377
11ThursdayGasE12-Nov-20234.15235242.65233.25241.45240.6239.03203518218424
12
13FridayGasE13-Nov-20240.55241.45245238.05242.5243.1242.23137915215327    
14MondayGasEopening stock350.75351
15TuesdayGasE17-Nov-20244.7246.25293.6242.55293.6292.3277.031514464115563346.345.6  Closing stock238.05238
16WednesdayGasE18-Nov-20292.3302.45350.75301339.9345.1333.4726647168331972103.45102.75  
17ThursdayGasE19-Nov-20345.1340.35346.8312.15331.4331.7333.8948732512539899.598.8  
18
19FridayGasE20-Nov-20331.7333.9361.5325.2343.6345.3348.061180378313130810.7510.5  
20MondayGasE23-Nov-20345.25358365.75340.95342344.7349.074884218641661514.75  opening stock365.75366
21TuesdayGasE24-Nov-20344.65348.8350.95337.5338.5338.8342.262207876347210.2   Closing stock311.95311
22WednesdayGasE25-Nov-20338.75341.6341.6320325.15324.9326.31354887353879    
23ThursdayGasE26-Nov-20324.9323.5324.25311.95315.8316.4316.37263654340577    
Sheet1
Cell Formulas
RangeFormula
T8,T20,T14T8=MAX(G7:G11)
U8,U20,U14U8=ROUNDUP(T8,0)
T9,T21,T15T9=MIN(H7:H11)
U9,U21,U15U9=ROUNDDOWN(T9,0)
O13,O19O13=IF(G13="","",IF(G13>=T8,G13-T8,""))
P13,P19P13=IF(G13="","",IF(G13>=U8,G13-U8,""))
Q13,Q19Q13=IF(G13="","",IF(H13<=T9,H13-T9,""))
R13,R19R13=IF(G13="","",IF(H13<=U9,H13-U9,""))
O15,O21O15=IF(G15="","",IF(G15>=T8,G15-T8,""))
P15,P21P15=IF(G15="","",IF(G15>=U8,G15-U8,""))
Q15,Q21Q15=IF(G15="","",IF(H15<=T9,H15-T9,""))
R15,R21R15=IF(G15="","",IF(H15<=U9,H15-U9,""))
O16,O22O16=IF(G16="","",IF(G16>=T8,G16-T8,""))
P16,P22P16=IF(G16="","",IF(G16>=U8,G16-U8,""))
Q16,Q22Q16=IF(G16="","",IF(H16<=T9,H16-T9,""))
R16,R22R16=IF(G16="","",IF(H16<=U9,H16-U9,""))
O17,O23O17=IF(G17="","",IF(G17>=T8,G17-T8,""))
P17,P23P17=IF(G17="","",IF(G17>=U8,G17-U8,""))
Q17,Q23Q17=IF(G17="","",IF(H17<=T9,H17-T9,""))
R17,R23R17=IF(G17="","",IF(H17<=U9,H17-U9,""))
O20O20=IF(G20="","",IF(G20>=T14,G20-T14,""))
P20P20=IF(G20="","",IF(G20>=U14,G20-U14,""))
Q20Q20=IF(G20="","",IF(H20<=T15,H20-T15,""))
R20R20=IF(G20="","",IF(H20<=U15,H20-U15,""))


guide me how can we do that
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this:

VBA Code:
Sub ReplaceFormulas()
Dim WS As Worksheet, i As Long, j As Long, Lr As Long, k As Long
Lr = Cells(Rows.Count, 1).End(xlUp).Row

For i = 1 To 3
k = 6 * i + 2
If k < Lr Then
Range("T" & k).Value = Application.WorksheetFunction.Max(Range("G" & k - 1 & ":G" & k + 3))
Range("U" & k).Value = Application.WorksheetFunction.RoundUp(Range("T" & k), 0)
Range("T" & k + 1).Value = Application.WorksheetFunction.Min(Range("H" & k - 1 & ":H" & k + 3))
Range("U" & k + 1).Value = Application.WorksheetFunction.RoundDown(Range("T" & k + 1), 0)
Else
Exit Sub
End If
Next i

For i = 13 To Lr
For j = 15 To 18
k = (Int((i - 5) / 7)) * 6 + 2
If j <= 16 Then
If Cells(i, 7).Value = "" Then
Cells(i, j).Value = ""
ElseIf Cells(i, 7).Value >= Cells(k, j + 5).Value Then
Cells(i, j).Value = Cells(i, 7).Value - Cells(k, j + 5).Value
Else
Cells(i, j).Value = ""
End If
Else
If Cells(i, 7).Value = "" Then
Cells(i, j).Value = ""
ElseIf Cells(i, 7).Value <= Cells(k + 1, j + 3).Value Then
Cells(i, j).Value = Cells(i, 7).Value - Cells(k + 1, j + 3).Value
Else
Cells(i, j).Value = ""
End If
End If
Next j
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

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