Clear Cache For Excel 2007 using VBA

PrashanthNair

New Member
Joined
Jun 10, 2010
Messages
21
Hi guys..

My name is Prashanth from Malaysia. I would like to know if it is possible to clear cache in excel 2007 using VBA. With the higher number of repetition of a calculation such as 1000 times, vba codes goes "Out of Memory". How do we clear this cache to resume the calculation?:outtahere:
 
Hi Prashanth & Welcome to the Board!

Can you provide the code that is giving this error and state in what circumstances it is being run and on which line you are receiving the error - thanks.
 
Upvote 0
Hi Richard..

Can you guide me on how to post my code in this forum??..sorry for the inconvenience..

my email add

thanks
 
Last edited by a moderator:
Upvote 0
thanks ya..Ok..My macro goes as follows...

Code:
Sub output_results()
    Dim sh As Worksheet, flg As Boolean
    'saving
        For Each sh In Worksheets
            If sh.Name Like "Simulation *" + CStr(count) Then flg = True: Exit For
        Next
        
        If flg = True Then
            If outcome_option = "FULL " Then
                'Sheets("Simulation " & count).Move after:=Worksheets(Worksheets.count)
                Sheets("Simulation " & count).Range("a1:h35000") = ""
                Sheets("Simulation " & count).Range("e3:e35000") = WorksheetFunction.Transpose(arraytesttestDays) ' my error starts here
                Sheets("Simulation " & count).Range("b3:b35000") = WorksheetFunction.Transpose(arraytesttestQg)
                Sheets("Simulation " & count).Range("c3:c35000") = WorksheetFunction.Transpose(arraytesttestQw)
                Sheets("Simulation " & count).Range("f3:f35000") = WorksheetFunction.Transpose(arraytesttestCumGas)
                Sheets("Simulation " & count).Range("g3:g35000") = WorksheetFunction.Transpose(arraytesttestCumWater)
                Sheets("Simulation " & count).Range("d3:d35000") = WorksheetFunction.Transpose(arraytesttestResPres)
                Sheets("Simulation " & count).Range("b2") = QG_title
                Sheets("Simulation " & count).Range("c2") = QW_title
                Sheets("Simulation " & count).Range("d2") = PRESSURE_title
                Sheets("Simulation " & count).Range("e2") = DAYS_title
                Sheets("Simulation " & count).Range("f2") = CUMGASPROD_title
                Sheets("Simulation " & count).Range("g2") = CUMWATERPROD_title
            ElseIf outcome_option = "SELECTED" Then
                Sheets("Simulation " & count).Range("a1:h35000") = ""
                Sheets("Simulation " & count).Range("b2") = "Initial water rate"
                Sheets("Simulation " & count).Range("d2") = QW_title
                Sheets("Simulation " & count).Range("b3") = "Peak water rate"
                Sheets("Simulation " & count).Range("d3") = QW_title
                Sheets("Simulation " & count).Range("b4") = "Initial gas rate"
                Sheets("Simulation " & count).Range("d4") = QG_title
                Sheets("Simulation " & count).Range("b5") = "Time to first gas"
                Sheets("Simulation " & count).Range("d5") = DAYS_title
                Sheets("Simulation " & count).Range("b6") = "Peak gas rate"
                Sheets("Simulation " & count).Range("d6") = QG_title
                Sheets("Simulation " & count).Range("b7") = "Time to peak "
                Sheets("Simulation " & count).Range("d7") = DAYS_title
                Sheets("Simulation " & count).Range("b8") = "Ultimate Recovery Gas"
                Sheets("Simulation " & count).Range("d8") = CUMGASPROD_title
                Sheets("Simulation " & count).Range("b9") = "Ultimate Recovery Water"
                Sheets("Simulation " & count).Range("d9") = CUMWATERPROD_title
                 
                For p2 = 1 To 34997
                    If arraytesttestQw(p2 - 1) = 0 And arraytesttestQw(p2) > 0 Then
                        Sheets("Simulation " & count).Range("c2:c3") = arraytesttestQw(p2)
                        Exit For
                    End If
                Next
                         
                 
                For p3 = 1 To 34997
                    If arraytesttestQg(p3 - 1) = 0 And arraytesttestQg(p3) > 0 Then
                        Sheets("Simulation " & count).Range("c4") = arraytesttestQg(p3)
                        Sheets("Simulation " & count).Range("c5") = arraytesttestDays(p3)
                        Exit For
                    End If
                Next
        
                For p4 = 1 To 34997
                    If arraytesttestQg(p4) = WorksheetFunction.Max(arraytesttestQg) Then
                        Sheets("Simulation " & count).Range("c6") = arraytesttestQg(p4)
                        Sheets("Simulation " & count).Range("c7") = arraytesttestDays(p4)
                         Exit For
                    End If
                Next
        
                Sheets("Simulation " & count).Range("c8") = WorksheetFunction.Max(arraytesttestCumGas)
                Sheets("Simulation " & count).Range("c9") = WorksheetFunction.Max(arraytesttestCumWater)
            End If
        
    Else
          If outcome_option = "FULL " Then
                Sheets.Add.Name = "Simulation " + CStr(count)
                Sheets("Simulation " & count).Move after:=Worksheets(Worksheets.count)
                Sheets("Simulation " & count).Range("e3:e35000") = WorksheetFunction.Transpose(arraytesttestDays)
                Sheets("Simulation " & count).Range("b3:b35000") = WorksheetFunction.Transpose(arraytesttestQg)
                Sheets("Simulation " & count).Range("c3:c35000") = WorksheetFunction.Transpose(arraytesttestQw)
                Sheets("Simulation " & count).Range("f3:f35000") = WorksheetFunction.Transpose(arraytesttestCumGas)
                Sheets("Simulation " & count).Range("g3:g35000") = WorksheetFunction.Transpose(arraytesttestCumWater)
                Sheets("Simulation " & count).Range("d3:d35000") = WorksheetFunction.Transpose(arraytesttestResPres)
                Sheets("Simulation " & count).Range("b2") = QG_title
                Sheets("Simulation " & count).Range("c2") = QW_title
                Sheets("Simulation " & count).Range("d2") = PRESSURE_title
                Sheets("Simulation " & count).Range("e2") = DAYS_title
                Sheets("Simulation " & count).Range("f2") = CUMGASPROD_title
                Sheets("Simulation " & count).Range("g2") = CUMWATERPROD_title
                
         ElseIf outcome_option = "SELECTED" Then
                Sheets.Add.Name = "Simulation " + CStr(count)
                Sheets("Simulation " & count).Move after:=Worksheets(Worksheets.count)
                Sheets("Simulation " & count).Range("b2") = "Initial water rate"
                Sheets("Simulation " & count).Range("d2") = QW_title
                Sheets("Simulation " & count).Range("b3") = "Peak water rate"
                Sheets("Simulation " & count).Range("d3") = QW_title
                Sheets("Simulation " & count).Range("b4") = "Initial gas rate"
                Sheets("Simulation " & count).Range("d4") = QG_title
                Sheets("Simulation " & count).Range("b5") = "Time to first gas"
                Sheets("Simulation " & count).Range("d5") = DAYS_title
                Sheets("Simulation " & count).Range("b6") = "Peak gas rate"
                Sheets("Simulation " & count).Range("d6") = QG_title
                Sheets("Simulation " & count).Range("b7") = "Time to peak "
                Sheets("Simulation " & count).Range("d7") = DAYS_title
                Sheets("Simulation " & count).Range("b8") = "Ultimate Recovery Gas"
                Sheets("Simulation " & count).Range("d8") = CUMGASPROD_title
                Sheets("Simulation " & count).Range("b9") = "Ultimate Recovery Water"
                Sheets("Simulation " & count).Range("d9") = CUMWATERPROD_title
                 
                For p2 = 1 To 34997
                    If arraytesttestQw(p2 - 1) = 0 And arraytesttestQw(p2) > 0 Then
                        Sheets("Simulation " & count).Range("c2:c3") = arraytesttestQw(p2)
                        Exit For
                    End If
                Next
                         
                 
                For p3 = 1 To 34997
                    If arraytesttestQg(p3 - 1) = 0 And arraytesttestQg(p3) > 0 Then
                        Sheets("Simulation " & count).Range("c4") = arraytesttestQg(p3)
                        Sheets("Simulation " & count).Range("c5") = arraytesttestDays(p3)
                        Exit For
                    End If
                Next
        
                For p4 = 1 To 34997
                    If arraytesttestQg(p4) = WorksheetFunction.Max(arraytesttestQg) Then
                        Sheets("Simulation " & count).Range("c6") = arraytesttestQg(p4)
                        Sheets("Simulation " & count).Range("c7") = arraytesttestDays(p4)
                         Exit For
                    End If
                Next
        
                Sheets("Simulation " & count).Range("c8") = WorksheetFunction.Max(arraytesttestCumGas)
                Sheets("Simulation " & count).Range("c9") = WorksheetFunction.Max(arraytestesttCumWater)
            End If
         
    End If

End Sub


This macro comes from another macro which calculates the values based on parameters set by user. It more like a forecast tool which helps to calculate the expected gas rate,water rate, cumulative water production as well as cumulative gas production of a known gas reservoir.

I was simulating for 1000 times using different parameters value randomly(monte-carlo simulation). At the 486th simulation, i received "Runtime error 1004- application defined or object defined error" and "Out of Memory" at the "worksheetfunction.transpose" code.

If i restart the calculation, the forecast tool will stop at much lower simulation(i.e 210th simulation).

Sorry if its very confusing as i don't know how to explain in well.
 
Last edited by a moderator:
Upvote 0
OK when it fails I need to know what the "arraytesttestDays" variable contains ie if it is an array, what is its source and how many dimensions does it contain and what the Ubound of each dimension is. I *suspect* that the problem you are seeing relates to this.
 
Upvote 0
Sorry for the late reply..This is the core calculation macro which stores the values into the array..Yup..i am exporting the results into separate sheets for easy view..it needs to go more than 1000 is possible since it is randomly generating create the parameters which can be used later on for History Matching..


Function mcroot(a3, a2, a1, a0)
'
' Computes the maximum real root of the cubic equation
' a3 x^3 + a2 x^2 + a1 x + a0 = 0
'
Dim Atest, btest, c, d, Ztest, ptest, qtest, Disc, h, theta, ytest, z1, z2, z3, c1, s1, M, rtest
Atest = a2 / a3
btest = a1 / a3
c = a0 / a3
ptest = (-Atest ^ 2 / 3 + btest) / 3
qtest = (9 * Atest * btest - 2 * Atest ^ 3 - 27 * c) / 54
Disc = qtest ^ 2 + ptest ^ 3
If Disc > 0 Then
h = qtest + Disc ^ (1 / 2)
ytest = (Abs(h)) ^ (1 / 3)
If h < 0 Then ytest = -ytest
Ztest = ytest - ptest / ytest - Atest / 3
Else
theta = Atn((-Disc) ^ (1 / 2) / qtest)
c1 = Cos(theta / 3)
If qtest < 0 Then
s1 = Sin(theta / 3)
c1 = (c1 - s1 * 3 ^ (1 / 2)) / 2
End If
z1 = 2 * (-ptest) ^ (1 / 2) * c1 - Atest / 3
M = Atest + z1
rtest = (M ^ 2 - 4 * (btest + M * z1)) ^ (1 / 2)
z2 = (-M + rtest) / 2
z3 = (-M - rtest) / 2
Ztest = z1
If z2 > Ztest Then Ztest = z2
If z3 > Ztest Then Ztest = z3
End If
mcroot = Ztest
End Function

Sub SimulationManager()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Qg_abd_type = Sheets("RiskAnalysis").Range("d63")
CWType = Sheets("RiskAnalysis").Range("s21")
Ztype = Sheets("RiskAnalysis").Range("q21")
Simulation_no = Sheets("RiskAnalysis").Range("e20")
InputType = Sheets("RiskAnalysis").Range("g21")
Outputtype = Sheets("RiskAnalysis").Range("i21")
viscostype = Sheets("RiskAnalysis").Range("m21")
BWtype = Sheets("RiskAnalysis").Range("o21")
Day_abd_type = Sheets("RiskAnalysis").Range("d64")
Form_of_Outcome = Sheets("RiskAnalysis").Range("k21")
outcome_2 = Sheets("RiskAnalysis").Range("i23")
plot_2 = Sheets("RiskAnalysis").Range("m23")
outcome_option = Sheets("RiskAnalysis").Range("g23")
tester = ""

test_type_of_distribution
If tester = "NO" Then
MsgBox ("try again")
Set tester = Nothing
Exit Sub
End If
For count = 1 To Simulation_no
Set QG_title = Nothing
Set QW_title = Nothing
Set PRESSURE_title = Nothing
Set DAYS_title = Nothing
Set CUMGASPROD_title = Nothing
Set CUMWATERPROD_title = Nothing
Set E = Nothing
Set A_Mix = Nothing
Set B_H2S = Nothing
Set CO2 = Nothing
Set H2S = Nothing
Set testpor = Nothing
Set testperm = Nothing
Set testbhp = Nothing
Set testqw = Nothing
Set testqg = Nothing
Set testcumgas = Nothing
Set testcumwater = Nothing
Set testrespres = Nothing
Set testrespres = Nothing
Set testsw = Nothing
Set testsg = Nothing
Set testkrw = Nothing
Set testkrg = Nothing
Set Qg_abd = Nothing
Set Day_abd = Nothing
Set ZMatBal = Nothing
Set peak_gas = Nothing
Set a_i = Nothing
Set w_i = Nothing
Set bulkdensity_i = Nothing
Set Poro_i = Nothing
Set mCH4_i = Nothing
Set mAir_i = Nothing
Set SGCH4_i = Nothing
Set Tc_i = Nothing
Set Pc_i = Nothing
Set We_i = Nothing
Set wt_i = Nothing
Set Swc_i = Nothing
Set nw_i = Nothing
Set krw0_i = Nothing
Set Sgc_i = Nothing
Set ng_i = Nothing
Set krg0_i = Nothing
Set el_i = Nothing
Set KdivideM_i = Nothing
'skin_i = Nothing
Set Swi_i = Nothing
Set Bw_i = Nothing
Set Miuwater_i = Nothing
Set VL_i = Nothing
Set PL_i = Nothing
Set Pi_i = Nothing
Set PKing_i = Nothing
Set Pwf_i = Nothing
Set delP_i = Nothing
Set GC_i = Nothing
Set area_i = Nothing
Set h_i = Nothing
Set Temp_i = Nothing
Set cw_i = Nothing
Set cf_i = Nothing
Set Perm_i = Nothing
Set rw_i = Nothing
Set gasconst_i = Nothing
Set waterconst_i = Nothing
Set Pabd_i = Nothing
Set cfnew_i = Nothing
Set areaft2 = Nothing
Set ResVol_i = Nothing
Set coalweight_i = Nothing
Set Zpwf = Nothing
Set miupwf = Nothing
Set WIIP_i = Nothing
Set re_i = Nothing
Set SeidleHuitt = Nothing
Set ShiDurucan = Nothing
Set kincline = Nothing
Set cm_Seidle = Nothing
Set n_Seidle = Nothing
Set n_PalMan = Nothing
Set MatrixShrink = Nothing
Set v_shi = Nothing
Set E_shi = Nothing
Set el_shi = Nothing
Set sigma_shi = Nothing
Set Pe_shi = Nothing
Set I_kincline = Nothing
Set delsigma = Nothing
Set Por_Rat = Nothing
Set Perm_Rat = Nothing
Set CleVol = Nothing
Set WaterVolInCleats = Nothing
Set GasVolInCleats = Nothing
Set GasVolInCleats_scf = Nothing
Set TotalFluid = Nothing
Set water_need_Rcf = Nothing
Set water_need_Scf = Nothing
Set CumWaterRcf = Nothing
Set CumWaterScf = Nothing
Set gas_need_Rcf = Nothing
Set gas_need_Scf = Nothing
Set CumGasRcf = Nothing
Set CumGasScf = Nothing
Set Pr = Nothing
Set Tr = Nothing
Set Asqr = Nothing
Set b = Nothing
Set r = Nothing
Set q = Nothing
Set Z = Nothing
Set f = Nothing
Set g = Nothing
Set k = Nothing
Set x = Nothing
Set y = Nothing
Set density = Nothing
Set miu = Nothing
Set Bg = Nothing
Set Psqr = Nothing
Set Bsqr = Nothing
Set krg = Nothing
Set cw = Nothing
Set Sw = Nothing
Set Sg = Nothing
Set krw = Nothing
Set QW = Nothing
Set BHP = Nothing
Set GC = Nothing
Set GC_des_Scf = Nothing
Set GC_des_Rcf = Nothing
Set CumGasDesRcf = Nothing
Set CumGasDesScf = Nothing
Set time_water = Nothing
Set time_gas = Nothing
Set Days_stream = Nothing
Set Gas_2_produce = Nothing
Set Water_2_produce = Nothing
Set Gas2Water = Nothing
Set Water2gas = Nothing
Set PrBHP = Nothing
Set AsqrBHP = Nothing
Set bBHP = Nothing
Set rBHP = Nothing
Set qBHP = Nothing
Set ZBHP = Nothing
Set fBHP = Nothing
Set gBHP = Nothing
Set CBHP = Nothing
Set DBHP = Nothing
Set EBHP = Nothing
Set kBHP = Nothing
Set xBHP = Nothing
Set yBHP = Nothing
Set densityBHP = Nothing
Set miuBHP = Nothing
Set PrBHP_Qw = Nothing
Set AsqrBHP_Qw = Nothing
Set bBHP_Qw = Nothing
Set rBHP_Qw = Nothing
Set qBHP_Qw = Nothing
Set ZBHP_Qw = Nothing
Set densityBHP_Qw = Nothing
Set miuBHP_Qw = Nothing
Set BsqrBHP_Qw = Nothing
Set maximumBHP = Nothing
Set QG = Nothing
Set QgActual = Nothing
Set BsqrActual = Nothing
Set Qw_new = Nothing
Set PrBHP_new = Nothing
Set AsqrBHP_new = Nothing
Set bBHP_new = Nothing
Set rBHP_new = Nothing
Set qBHP_new = Nothing
Set ZBHP_new = Nothing
Set fBHP_new = Nothing
Set gBHP_new = Nothing
Set CBHP_new = Nothing
Set DBHP_new = Nothing
Set EBHP_new = Nothing
Set kBHP_new = Nothing
Set xBHP_new = Nothing
Set yBHP_new = Nothing
Set densityBHP_new = Nothing
Set miuBHP_new = Nothing
Set Qg_new = Nothing
Set PrMaximumBHP = Nothing
Set AsqrMaximumBHP = Nothing
Set bMaximumBHP = Nothing
Set rMaximumBHP = Nothing
Set qMaximumBHP = Nothing
Set ZMaximumBHP = Nothing
Set densityMaximumBHP = Nothing
Set miuMaximumBHP = Nothing
Set BsqrMaximumBHP = Nothing
Set PrBHP_2 = Nothing
Set AsqrBHP_2 = Nothing
Set bBHP_2 = Nothing
Set rBHP_2 = Nothing
Set qBHP_2 = Nothing
Set ZBHP_2 = Nothing
Set densityBHP_2 = Nothing
Set miuBHP_2 = Nothing
Set BsqrBHP = Nothing
Set p = Nothing
Set delPresNew = Nothing
Set Pr_initial = Nothing
Set Tr_initial = Nothing
Set Asqr_initial = Nothing
Set b_initial = Nothing
Set r_initial = Nothing
Set q_initial = Nothing
Set Z_initial = Nothing
Set Bg_initial = Nothing
Set totalgas = Nothing
Set PalMan_i = Nothing
Set Tr = Nothing
Set k = Nothing
Set x = Nothing
Set y = Nothing
Set GCabd_i = Nothing
Set Recover_GC_Iso = Nothing
Set RF_Iso = Nothing
Set Pres_Iso = Nothing
Set GC_Iso = Nothing
Set LangIso_i = Nothing
Set RelPerm_i = Nothing
Set outcome_i = Nothing
Set Sw_i = Nothing
Set krw_i = Nothing
Set krwcorrect_i = Nothing
Set Sg_i = Nothing
Set krg_i = Nothing
Set krgcorrect_i = Nothing
Set check_GC = Nothing
Set outcomeform = Nothing
Erase arraytestCH4density
Erase arraytestZMatBal
Erase arraytesttestBHP
Erase arraytestBHP_output
Erase arraytestGC
Erase arraytestinitial_gas
Erase arraytestSelectedDays
Erase arraytestPres_Iso
Erase arraytestGC_Iso
Erase arraytestdelPres
Erase arraytestResPres
Erase arraytestCumGasDesRcf
Erase arraytestCumGasDesScf
Erase arraytestBg
Erase arraytestKrw
Erase arraytestKrg
Erase arraytestGas2Water
Erase arraytestWater2Gas
Erase arraytestWaterVolInCleats
Erase arraytestGasVolInCleats
Erase arraytestGasVolInCleats_scf
Erase arraytestWaterNeed2Produce
Erase arraytestWaterNeed2Produce_Scf
Erase arraytestCumWaterRcf
Erase arraytestCumWaterScf
Erase arraytestGasNeed2Produce
Erase arraytestGasNeed2Produce_Scf
Erase arraytestCumGasRcf
Erase arraytestCumGasScf
Erase arraytestDays
Erase arraytestZ
Erase arraytestGC_des_Scf
Erase arraytestGC_des_Rcf
Erase arraytestmiu
Erase arraytestcw
Erase arraytestPsqr
Erase arraytestSw
Erase arraytestSg
Erase arraytestGas_2_produce
Erase arraytestWater_2_produce
Erase arraytestPor_Rat
Erase arraytestNewPoro
Erase arraytestPerm_Rat
Erase arraytestNewPerm
Erase arraytestTotalFluid
Erase arraytestwater_need_Scf
Erase arraytestgas_need_Scf
Erase arraytestQw
Erase arraytestQw_scf
Erase arraytestBHP
Erase arraytesttime_water
Erase arraytesttime_gas
Erase arraytestDays_stream
Erase arraytestCleVol
Erase arraytestQg
Erase arraytestBsqr
Erase arraytesttotalgas
Erase arraytestResPres_output
Erase arraytestDays_output
Erase arraytestQg_output
Erase arraytestQw_output
Erase arraytestCumGas_output
Erase arraytestCumWater_output
Erase arraytestSw_i
Erase arraytestSg_i
Erase arraytestKrw_act
Erase arraytestKrw_i
Erase arraytestKrg_act
Erase arraytestKrg_i
Erase arraytesttestDays
Erase arraytesttestQg
Erase arraytesttestQw
Erase arraytesttestCumGas
Erase arraytesttestCumWater
Erase arraytesttestResPres
Erase arraytesttestSw
Erase arraytesttestSg
Erase arraytesttestKrw
Erase arraytesttestKrg
Erase arraytesttestGC
Erase arraytestGC_output
Erase arraytestPorRat_output
Erase arraytesttestPorRat
Erase arraytestPermRat_output
Erase arraytesttestPermRat

testinput

'calculate initial cleat volume
arraytestCleVol(0) = (1 - cf_i * (Pi_i - Pi_i)) * ResVol_i * Poro_i / 100

' calculate initial water volume & gas volume & total fluids in cleats
arraytestWaterVolInCleats(0) = area_i * 43560 * h_i * Swi_i * Poro_i / (100)
arraytestGasVolInCleats(0) = 0
arraytestTotalFluid(0) = arraytestGasVolInCleats(0) + arraytestWaterVolInCleats(0)

arraytestdelPres(0) = delP_i
arraytestResPres(0) = Pi_i

'calculate initial Palmer Mansouri perm & poro
If MatrixShrink = "YES" Then
If PalMan_i = "YES" Then
arraytestPor_Rat(0) = (1 + (cfnew_i / (Poro_i / 100)) * (Pi_i - Pi_i) + ((el_i) / (Poro_i / 100)) * (KdivideM_i - 1) * (Pi_i / (PL_i + Pi_i) - Pi_i / (PL_i + Pi_i)))
arraytestNewPoro(0) = arraytestPor_Rat(0) * (Poro_i / 100)
arraytestPerm_Rat(0) = WorksheetFunction.Power(arraytestPor_Rat(0), n_PalMan)
arraytestNewPerm(0) = Perm_i * arraytestPerm_Rat(0)

ElseIf SeidleHuitt = "YES" Then
arraytestPor_Rat(0) = 1 + (1 + 2 / (Poro_i / 100)) * (cm_Seidle) * (10 ^ -6) * VL_i * (Pi_i / (PL_i + Pi_i) - Pi_i / (PL_i + Pi_i))
arraytestNewPoro(0) = arraytestPor_Rat(0) * (Poro_i / 100)
arraytestPerm_Rat(0) = WorksheetFunction.Power(arraytestPor_Rat(0), n_Seidle)
arraytestNewPerm(0) = Perm_i * arraytestPerm_Rat(0)

ElseIf ShiDurucan = "YES" Then
delsigma = (-v_shi / (1 - v_shi)) * (Pi_i - Pi_i)
arraytestNewPerm(0) = Perm_i * Exp(-3 * cf_i * delsigma)
ElseIf kincline = "YES" Then
arraytestNewPerm(0) = Exp(I_kincline / 100 * (Pi_i - Pi_i)) * Perm_i
End If
End If

' calculate initial gas content
If Pi_i >= PKing_i Then
arraytestGC(0) = (VL_i * PKing_i) / (PL_i + PKing_i)
Else: arraytestGC(0) = (VL_i * Pi_i) / (PL_i + Pi_i)
End If

arraytestCumGasDesScf(0) = 0
arraytestCumGasDesRcf(0) = 0

'calculate initial Bg and Bw
Pr_initial = (Pi_i) / (Pc_i + 14.6959488)
Asqr_initial = 0.42747 * (Pr_initial / ((((Temp_i + 460) / 1.8) / Tc_i) ^ 2.5))
b_initial = 0.08664 * (Pr_initial / (((Temp_i + 460) / 1.8) / Tc_i))
r_initial = Asqr_initial * b_initial
q_initial = (b_initial ^ 2) + b_initial - Asqr_initial
If Ztype = "Redlich-Kwong" Then
Z_initial = mcroot(1, -1, -q_initial, -r_initial)
ElseIf Ztype = "Brill and Beggs Model" Then
Z_initial = (1.39 * ((((Temp_i + 460) / 1.8) / Tc_i) - 0.92) ^ 0.5 - 0.36 * (((Temp_i + 460) / 1.8) / Tc_i) - 0.101) + ((1 - (1.39 * ((((Temp_i + 460) / 1.8) / Tc_i) - 0.92) ^ 0.5 - 0.36 * (((Temp_i + 460) / 1.8) / Tc_i) - 0.101)) / (Exp((0.62 - 0.23 * (((Temp_i + 460) / 1.8) / Tc_i)) * ((Pi_i) / (Pc_i + 14.6959488)) + ((0.066) / ((((Temp_i + 460) / 1.8) / Tc_i) - 0.86) - 0.037) * (((Pi_i) / (Pc_i + 14.6959488)) ^ 2)))) + ((0.132 - 0.32 * WorksheetFunction.Log10((((Temp_i + 460) / 1.8) / Tc_i))) * (((Pi_i) / (Pc_i + 14.6959488)) ^ (10 ^ (0.3106 - 0.49 * (((Temp_i + 460) / 1.8) / Tc_i) + 0.1824 * ((((Temp_i + 460) / 1.8) / Tc_i) ^ 2)))))
End If
arraytestBg(0) = 0.0283 * Z_initial * (Temp_i + 460) / Pi_i
If BWtype = "Constant" Then
arraytestBw(0) = Bw_i
ElseIf BWtype = "Pres&Temp effect" Then
arraytestBw(0) = (1 + ((-0.010001) + (0.000133391) * (Temp_i) + (0.000000550654) * ((Temp_i) ^ 2))) * (1 + ((-0.00000000195301) * (Pi_i) * (Temp_i) - 1.72834E-13 * ((Pi_i) ^ 2 * Temp_i) - 0.000000358922 * (Pi_i) - 0.000000000225341 * (Pi_i) ^ 2))
End If

If MatBalType = "King" Then
arraytestZMatBal(0) = (Pi_i) / ((Z_initial) / (((bulkdensity_i * arraytestBg(0)) / (Poro_i / 100) * (VL_i * Pi_i) / (PL_i + Pi_i))))
ElseIf MatBalType = "Seidle" Then
arraytestZMatBal(0) = Pi_i / ((Z_initial) / (((bulkdensity_i * arraytestBg(0)) / (Poro_i / 100) * (VL_i * Pi_i) / (PL_i + Pi_i))))
ElseIf MatBalType = "Jensen and Smith" Then
arraytestZMatBal(0) = (Pi_i) / (PL_i + Pi_i)
End If


arraytestWaterNeed2Produce(0) = 0
arraytestwater_need_Scf(0) = arraytestWaterNeed2Produce(0) / arraytestBw(0)
arraytestCumWaterRcf(0) = 0
arraytestCumWaterScf(0) = 0
arraytestGasNeed2Produce(0) = 0
arraytestgas_need_Scf(0) = arraytestGasNeed2Produce(0) / arraytestBg(0)
arraytestGasVolInCleats_scf(0) = arraytestGasVolInCleats(0) / arraytestBg(0)
arraytesttotalgas(0) = arraytestGC(0) * coalweight_i + arraytestGasNeed2Produce(0) + arraytestGasVolInCleats_scf(0)
arraytestCumGasRcf(0) = 0
arraytestCumGasScf(0) = 0
arraytestDays(0) = 0
arraytestQw(0) = 0
arraytestQw_scf(0) = arraytestQw(0) * 5.61458333333333
arraytestQg(0) = 0
maxQg = arraytestQg(0)
arraytesttime_water(0) = 0
arraytesttime_gas(0) = 0
arraytestBHP(0) = Pwf_i

'calculate initial Krg & Krw
If MatrixShrink = "NO" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw_i * (Pi_i - Pi_i)) + ((5.61458333333333 * We_i - arraytestCumWaterRcf(0)) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - Pi_i)) * 100))
arraytestSw(0) = Sw
Else
If PalMan_i = "YES" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw_i * (Pi_i - Pi_i)) + ((5.61458333333333 * We_i - arraytestCumWaterRcf(0)) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cfnew_i * (Pi_i - Pi_i)) * 100))
arraytestSw(0) = Sw
Else
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw_i * (Pi_i - Pi_i)) + ((5.61458333333333 * We_i - arraytestCumWaterRcf(0)) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - Pi_i)) * 100))
arraytestSw(0) = Sw
End If
End If
If corey_i = "YES" Then
If (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i < 0 Then
arraytestKrw(0) = 0
Else: arraytestKrw(0) = (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i
End If
Sg = 100 - Sw
arraytestSg(0) = Sg
If Sg <= Sgc_i Then
arraytestKrg(0) = 0
ElseIf ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i > krg0_i Then
arraytestKrg(0) = krg0_i
Else: arraytestKrg(0) = ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i
End If
ElseIf honarpour_i = "YES" Then
If (0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * ((Sw / 100) ^ 3.6) * (Sw / 100 - Swc_i / 100))) < 0 Then
arraytestKrw(0) = 0
Else: arraytestKrw(0) = (0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * ((Sw / 100) ^ 3.6) * (Sw / 100 - Swc_i / 100)))
End If
Sg = 100 - Sw
arraytestSg(0) = Sg
If Sg <= Sgc_i Then
arraytestKrg(0) = 0
Else
If arraytestKrg(0) > 1 Then
arraytestKrg(0) = 1
Else: arraytestKrg(0) = 1.1072 * ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100)) ^ 2
End If
End If
End If

'initial gas to water and water to gas ratios
arraytestGas2Water(0) = 0
arraytestWater2Gas(0) = 1

Tr = (5 / 9 * (Temp_i - 32) + 273) / (Tc_i)
If viscostype = "Lee et. Al" Then
k = (9.4 + 0.02 * mCH4_i) * (((Temp_i + 460) ^ 1.5) / (209.2 + 19.3 * mCH4_i + (Temp_i + 460)))
x = (3.5 + (986 / (Temp_i + 460)) + 0.01 * mCH4_i)
y = 2.4 - 0.2 * x
ElseIf viscostype = "Lee Gonzalez & Eakin" Then
k = ((9.379 + 0.01607 * mCH4_i) * (Temp_i + 460) ^ 1.5) / (209.2 + 19.26 * mCH4_i + (Temp_i + 460))
x = 3.448 + (986.4 / (Temp_i + 460)) + (0.01009) * (mCH4_i)
y = 2.447 - (0.2224) * (x)
ElseIf viscostype = "Optimized Lee Gonzalez Eakian" Then
k = ((19.9216 + 0.0326212 * mCH4_i) * (Temp_i + 460) ^ 1.38392) / (210.076 + 18.5762 * mCH4_i + (Temp_i + 460))
x = 3.84699 + (991.303 / (Temp_i + 460)) + (0.00924455) * (mCH4_i)
y = 2.11068 - (0.136279) * (x)
End If
BsqrActual = (Pwf_i ^ 2) / (Zpwf * miupwf)
arraytestBsqr(0) = BsqrActual
arraytestBHP(0) = Pwf_i


For i = 1 To 34997
'calculate Reservoir Pressure
If arraytestResPres(i - 1) - arraytestdelPres(i - 1) > Pabd_i Then
If arraytestResPres(i - 1) - arraytestdelPres(i - 1) <= PKing_i Then
delPresNew = ((PL_i * ((0.01 * arraytestCleVol(i - 1)) / (coalweight_i * arraytestBg(i - 1)) - arraytestGC(i - 1))) + (arraytestResPres(i - 1) * ((0.01 * arraytestCleVol(i - 1)) / (coalweight_i * arraytestBg(i - 1)) - arraytestGC(i - 1))) + (VL_i * arraytestResPres(i - 1))) / (((0.01 * arraytestCleVol(i - 1)) / (coalweight_i * arraytestBg(i - 1)) - arraytestGC(i - 1)) + VL_i)
p = arraytestResPres(i - 1) - delPresNew
arraytestdelPres(i) = delPresNew
Else:
delPresNew = delP_i
p = arraytestResPres(i - 1) - delPresNew
arraytestdelPres(i) = delPresNew
End If
ElseIf arraytestResPres(i - 1) - arraytestdelPres(i - 1) < Pabd_i Then
p = Pabd_i
End If

'calculate fluid properties
Pr = (p) / (Pc_i + 14.6959488)
Asqr = 0.42747 * (Pr / (Tr ^ 2.5))
b = 0.08664 * (Pr / Tr)
r = Asqr * b
q = (b ^ 2) + b - Asqr
If Ztype = "Redlich-Kwong" Then
Z = mcroot(1, -1, -q, -r)
ElseIf Ztype = "Brill and Beggs Model" Then
Z = (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101) + ((1 - (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101)) / (Exp((0.62 - 0.23 * Tr) * Pr + ((0.066) / (Tr - 0.86) - 0.037) * (Pr ^ 2)))) + ((0.132 - 0.32 * WorksheetFunction.Log10(Tr)) * (Pr ^ (10 ^ (0.3106 - 0.49 * Tr + 0.1824 * (Tr ^ 2)))))
End If
Bg = 0.0283 * Z * (Temp_i + 460) / p
If BWtype = "Constant" Then
Bw = Bw_i
ElseIf BWtype = "Pres&Temp effect" Then
Bw = (1 + ((-0.010001) + (0.000133391) * (Temp_i) + (0.000000550654) * ((Temp_i) ^ 2))) * (1 + ((-0.00000000195301) * (Pi_i) * (Temp_i) - 1.72834E-13 * ((Pi_i) ^ 2 * Temp_i) - 0.000000358922 * (Pi_i) - 0.000000000225341 * (Pi_i) ^ 2))
End If
density = (0.0433 * (p) * SGCH4_i / (Z * (Temp_i + 460)))
miu = (k * 10 ^ -4) * Exp(x * (density ^ y))
Psqr = ((p) ^ 2) / (miu * Z)
krg = arraytestKrg(i - 1)

If CWType = "Salinity Effect" Then
If wt_i = 0 Then
cw = cw_i * 1
ElseIf wt_i > 0 Then
If p = 0 Then
cw = cw_i * (-0.015 * ((wt_i) / 100) + 0.993)
ElseIf p = 10000 Then
cw = cw_i * (-0.013 * ((wt_i) / 100) + 0.997)
ElseIf p = 20000 Then
cw = cw_i * (-0.011 * ((wt_i) / 100) + 0.998)
ElseIf p > 20000 Then
cw = cw_i * 1
End If
End If
ElseIf CWType = "Constant" Then
cw = cw_i
End If

' calculate gas content
If p >= PKing_i Then
GC = (VL_i * PKing_i) / (PL_i + PKing_i)
Else: GC = (VL_i * p) / (PL_i + p)
End If
GC_des_Scf = (arraytestGC(i - 1) - GC) * coalweight_i
GC_des_Rcf = GC_des_Scf * Bg
CumGasDesScf = GC_des_Scf + arraytestCumGasDesScf(i - 1)
CumGasDesRcf = GC_des_Rcf + arraytestCumGasDesRcf(i - 1)
arraytestGC(i) = GC
arraytestCumGasDesScf(i) = CumGasDesScf
arraytestCumGasDesRcf(i) = CumGasDesRcf


'calculate porosity and permeability ratios
If MatrixShrink = "YES" Then
If PalMan_i = "YES" Then
Por_Rat = (1 + (cfnew_i / (Poro_i / 100)) * (p - Pi_i) + ((el_i) / (Poro_i / 100)) * (KdivideM_i - 1) * (p / (PL_i + p) - Pi_i / (PL_i + Pi_i)))
Perm_Rat = (Por_Rat) ^ n_PalMan
ElseIf SeidleHuitt = "YES" Then
Por_Rat = 1 + (1 + 2 / (Poro_i / 100)) * (cm_Seidle * 10 ^ -6) * VL_i * (Pi_i / (PL_i + Pi_i) - p / (PL_i + p))
Perm_Rat = (Por_Rat) ^ n_Seidle
ElseIf ShiDurucan = "YES" Then
If p > PKing_i Then
delsigma = (-v_shi / (1 - v_shi)) * (p - Pi_i)
Perm_Rat = Exp(-3 * cf_i * delsigma)
ElseIf p < PKing_i Then
delsigma = (-v_shi / (1 - v_shi)) * (p - PKing_i) + (E_shi / (3 - 3 * v_shi)) * (el_shi) * ((p / (p + PKing_i)) - (PKing_i / (PKing_i + Pe_shi))) + (v_shi / (1 - v_shi)) * (PKing_i - Pi_i)
Perm_Rat = Exp(-3 * cf_i * delsigma)
End If
ElseIf kincline = "YES" Then
Perm_Rat = Exp(I_kincline / 100 * (Pi_i - p))
End If
End If
'calculate gas water ratios
If MatrixShrink = "NO" Then
Gas_2_produce = (arraytestKrg(i - 1) * Perm_i * h_i * (Psqr - BsqrActual)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * Perm_i * h_i * (p - Pwf_i)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
Else: Gas_2_produce = (arraytestKrg(i - 1) * (Perm_Rat * Perm_i) * h_i * (Psqr - BsqrActual)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - Pwf_i)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
End If
If Gas_2_produce + Water_2_produce <= 0 Then
Gas2Water = arraytestGas2Water(i - 1)
Water2gas = arraytestWater2Gas(i - 1)
Else: Gas2Water = (Gas_2_produce) / (Gas_2_produce + Water_2_produce)
Water2gas = (Water_2_produce) / (Gas_2_produce + Water_2_produce)
End If


'calculate cleat volume
If MatrixShrink = "NO" Then
CleVol = (1 - cf_i * (Pi_i - p)) * ResVol_i * Poro_i / 100
Else:
If PalMan_i = "YES" Then
CleVol = (1 - cfnew_i * (Pi_i - p)) * ResVol_i * Poro_i / 100
Else
CleVol = (1 - cf_i * (Pi_i - p)) * ResVol_i * Poro_i / 100
End If
End If

'calculate fluid volume in cleats
WaterVolInCleats = arraytestWaterVolInCleats(i - 1) / arraytestBw(i - 1) * Bw - arraytestWaterNeed2Produce(i - 1)
GasVolInCleats = arraytestGasVolInCleats(i - 1) / arraytestBg(i - 1) * Bg + GC_des_Rcf - arraytestGasNeed2Produce(i - 1)
GasVolInCleats_scf = GasVolInCleats / Bg
TotalFluid = WaterVolInCleats + GasVolInCleats

'calculate cum water and cum gas
water_need_Rcf = (TotalFluid - CleVol) * Water2gas
water_need_Scf = water_need_Rcf / Bw
CumWaterRcf = water_need_Rcf + arraytestCumWaterRcf(i - 1)
CumWaterScf = water_need_Scf + arraytestCumWaterScf(i - 1)
gas_need_Rcf = (TotalFluid - CleVol) * Gas2Water
gas_need_Scf = gas_need_Rcf / Bg
CumGasRcf = gas_need_Rcf + arraytestCumGasRcf(i - 1)
CumGasScf = gas_need_Scf + arraytestCumGasScf(i - 1)

'calculate total gas in system(OGIP)
totalgas = GC * (coalweight_i) + gas_need_Scf + GasVolInCleats_scf

'calculate saturations & permeability
If MatrixShrink = "NO" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
Else
If PalMan_i = "YES" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cfnew_i * (Pi_i - p)) * 100))
Else
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
End If
End If
If corey_i = "YES" Then
If (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i < 0 Then
krw = 0
Else: krw = (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
ElseIf ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i > krg0_i Then
krg = krg0_i
Else: krg = (((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i)
End If
ElseIf honarpour_i = "YES" Then
If (0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * ((Sw / 100) ^ 3.6) * (Sw / 100 - Swc_i / 100))) < 0 Then
krw = 0
Else: krw = (0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * ((Sw / 100) ^ 3.6) * (Sw / 100 - Swc_i / 100)))
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
Else
If krg > 1 Then
krg = 1
Else: krg = 1.1072 * ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100)) ^ 2
End If
End If
End If

If MatBalType = "King" Then
ZMatBal = p / ((Z) / ((1 - cf_i * (Pi_i - p)) * (1 - Sw) + ((bulkdensity_i * Bg) / (Poro_i / 100) * (VL_i * p) / (PL_i + p))))
ElseIf MatBalType = "Seidle" Then
ZMatBal = p / ((Z) / ((1 - Sw) + ((bulkdensity_i * Bg) / (Poro_i / 100) * (VL_i * p) / (PL_i + p))))
ElseIf MatBalType = "Jensen and Smith" Then
ZMatBal = p / (PL_i + p)
End If

'calculate Qw and BHP needed
If MatrixShrink = "NO" Then
QW = (arraytestKrw(i - 1) * Perm_i * h_i * (p - Pwf_i)) / (141.2 * Miuwater_i * Bw_i * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))

If (QW * 5.61458333333333) >= waterconst_i Then
If MatrixShrink = "NO" Then
BHP = p - ((waterconst_i / 5.61458333333333 * 141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) / (arraytestKrw(i - 1) * Perm_i * h_i))
Qw_wanted = (arraytestKrw(i - 1) * Perm_i * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))
QW = Qw_wanted
arraytestBHP(i) = BHP
Else: BHP = p - ((waterconst_i / 5.61458333333333 * 141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) / (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i))
Qw_wanted = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))
QW = Qw_wanted
arraytestBHP(i) = BHP
End If
ElseIf (QW * 5.61458333333333) < waterconst_i Then
BHP = Pwf_i
arraytestBHP(i) = BHP
End If

'Re-calculate gas water ratio
arraytestBsqr(i) = BsqrActual
If BHP > Pwf_i Then
PrBHP_Qw = (((BHP)) / (Pc_i + 14.6959488))
AsqrBHP_Qw = 0.42747 * (PrBHP_Qw / (Tr ^ 2.5))
bBHP_Qw = 0.08664 * (PrBHP_Qw / Tr)
rBHP_Qw = AsqrBHP_Qw * bBHP_Qw
qBHP_Qw = (bBHP_Qw ^ 2) + bBHP_Qw - AsqrBHP_Qw
If Ztype = "Redlich-Kwong" Then
ZBHP_Qw = mcroot(1, -1, -qBHP_Qw, -rBHP_Qw)
ElseIf Ztype = "Brill and Beggs Model" Then
ZBHP_Qw = (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101) + ((1 - (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101)) / (Exp((0.62 - 0.23 * Tr) * PrBHP_Qw + ((0.066) / (Tr - 0.86) - 0.037) * (PrBHP_Qw ^ 2)))) + ((0.132 - 0.32 * WorksheetFunction.Log10(Tr)) * (PrBHP_Qw ^ (10 ^ (0.3106 - 0.49 * Tr + 0.1824 * (Tr ^ 2)))))
End If
densityBHP_Qw = (0.0433 * (BHP) * SGCH4_i / (ZBHP_Qw * (Temp_i + 460)))
miuBHP_Qw = (k * 10 ^ -4) * Exp(x * (densityBHP_Qw ^ y))
BsqrBHP_Qw = ((BHP) ^ 2) / (ZBHP_Qw * miuBHP_Qw)
arraytestBsqr(i) = BsqrBHP_Qw
If MatrixShrink = "NO" Then
Gas_2_produce = (arraytestKrg(i - 1) * Perm_i * h_i * (Psqr - BsqrBHP_Qw)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * Perm_i * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
Else: Gas_2_produce = (arraytestKrg(i - 1) * (Perm_Rat * Perm_i) * h_i * (Psqr - BsqrBHP_Qw)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
End If
If Gas_2_produce + Water_2_produce = 0 Then
Gas2Water = arraytestGas2Water(i - 1)
Water2gas = arraytestWater2Gas(i - 1)
Else: Gas2Water = (Gas_2_produce) / (Gas_2_produce + Water_2_produce)
Water2gas = (Water_2_produce) / (Gas_2_produce + Water_2_produce)
End If

'Re-calculate cum water and cum gas
water_need_Rcf = (TotalFluid - CleVol) * Water2gas
water_need_Scf = water_need_Rcf / Bw_i
CumWaterRcf = water_need_Rcf + arraytestCumWaterRcf(i - 1)
CumWaterScf = water_need_Scf + arraytestCumWaterScf(i - 1)
gas_need_Rcf = (TotalFluid - CleVol) * Gas2Water
gas_need_Scf = gas_need_Rcf / Bg
CumGasRcf = gas_need_Rcf + arraytestCumGasRcf(i - 1)
CumGasScf = gas_need_Scf + arraytestCumGasScf(i - 1)

'Re-calculate saturations & permeability
If MatrixShrink = "NO" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
Else
If PalMan_i = "YES" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cfnew_i * (Pi_i - p)) * 100))
Else
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
End If
End If
If corey_i = "YES" Then
If (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i < 0 Then
krw = 0
Else: krw = (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
ElseIf ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i > krg0_i Then
krg = krg0_i
Else: krg = ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i
End If
ElseIf honarpour_i = "YES" Then
If 0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * (Sw / 100) ^ 3.6) * ((Sw / 100 - Swc_i / 100)) < 0 Then
krw = 0
Else: krw = 0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * (Sw / 100) ^ 3.6) * ((Sw / 100 - Swc_i / 100))
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
Else
If krg > 1 Then
krg = 1
Else: krg = 1.1072 * ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100)) ^ 2
End If
End If
End If

ElseIf BHP < Pwf_i Then
End If


Else: QW = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - Pwf_i)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))
If (QW * 5.61458333333333) >= waterconst_i Then
If MatrixShrink = "NO" Then
BHP = p - ((waterconst_i / 5.61458333333333 * 141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) / (arraytestKrw(i - 1) * Perm_i * h_i))
Qw_wanted = (arraytestKrw(i - 1) * Perm_i * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))
QW = Qw_wanted
arraytestBHP(i) = BHP
Else: BHP = p - ((waterconst_i / 5.61458333333333 * 141.2 * Miuwater_i * Bw_i * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) / (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i))
Qw_wanted = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw_i * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))
QW = Qw_wanted
arraytestBHP(i) = BHP
End If
ElseIf (QW * 5.61458333333333) < waterconst_i Then
BHP = Pwf_i
arraytestBHP(i) = BHP
End If

'Re-calculate gas water ratio
If BHP > Pwf_i Then
PrBHP_Qw = ((BHP)) / (Pc_i + 14.6959488)
AsqrBHP_Qw = 0.42747 * (PrBHP_Qw / (Tr ^ 2.5))
bBHP_Qw = 0.08664 * (PrBHP_Qw / Tr)
rBHP_Qw = AsqrBHP_Qw * bBHP_Qw
qBHP_Qw = (bBHP_Qw ^ 2) + bBHP_Qw - AsqrBHP_Qw
If Ztype = "Redlich-Kwong" Then
ZBHP_Qw = mcroot(1, -1, -qBHP_Qw, -rBHP_Qw)
ElseIf Ztype = "Brill and Beggs Model" Then
ZBHP_Qw = (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101) + ((1 - (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101)) / (Exp((0.62 - 0.23 * Tr) * PrBHP_Qw + ((0.066) / (Tr - 0.86) - 0.037) * (PrBHP_Qw ^ 2)))) + ((0.132 - 0.32 * WorksheetFunction.Log10(Tr)) * (PrBHP_Qw ^ (10 ^ (0.3106 - 0.49 * Tr + 0.1824 * (Tr ^ 2)))))
End If
densityBHP_Qw = (0.0433 * (BHP) * SGCH4_i / (ZBHP_Qw * (Temp_i + 460)))
miuBHP_Qw = (k * 10 ^ -4) * Exp(x * (densityBHP_Qw ^ y))
BsqrBHP_Qw = ((BHP) ^ 2) / (ZBHP_Qw * miuBHP_Qw)
arraytestBsqr(i) = BsqrBHP_Qw
If MatrixShrink = "NO" Then
Gas_2_produce = (arraytestKrg(i - 1) * Perm_i * h_i * (Psqr - BsqrBHP_Qw)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * Perm_i * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
Else: Gas_2_produce = (arraytestKrg(i - 1) * (Perm_Rat * Perm_i) * h_i * (Psqr - BsqrBHP_Qw)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - BHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
End If
If Gas_2_produce + Water_2_produce = 0 Then
Gas2Water = arraytestGas2Water(i - 1)
Water2gas = arraytestWater2Gas(i - 1)
Else: Gas2Water = (Gas_2_produce) / (Gas_2_produce + Water_2_produce)
Water2gas = (Water_2_produce) / (Gas_2_produce + Water_2_produce)
End If

'Re-calculate cum water and cum gas
water_need_Rcf = (TotalFluid - CleVol) * Water2gas
water_need_Scf = water_need_Rcf / Bw_i
CumWaterRcf = water_need_Rcf + arraytestCumWaterRcf(i - 1)
CumWaterScf = water_need_Scf + arraytestCumWaterScf(i - 1)
gas_need_Rcf = (TotalFluid - CleVol) * Gas2Water
gas_need_Scf = gas_need_Rcf / Bg
CumGasRcf = gas_need_Rcf + arraytestCumGasRcf(i - 1)
CumGasScf = gas_need_Scf + arraytestCumGasScf(i - 1)

'Re-calculate saturations & permeability
If MatrixShrink = "NO" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
Else
If PalMan_i = "YES" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cfnew_i * (Pi_i - p)) * 100))
Else
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
End If
End If
If corey_i = "YES" Then
If (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i < 0 Then
krw = 0
Else: krw = (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
ElseIf ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i > krg0_i Then
krg = krg0_i
Else: krg = ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i
End If
ElseIf honarpour_i = "YES" Then
If 0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * (Sw / 100) ^ 3.6) * ((Sw / 100 - Swc_i / 100)) < 0 Then
krw = 0
Else: krw = 0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * (Sw / 100) ^ 3.6) * ((Sw / 100 - Swc_i / 100))
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
Else
If krg > 1 Then
krg = 1
Else: krg = 1.1072 * ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100)) ^ 2
End If

End If
End If

ElseIf BHP < Pwf_i Then
End If


End If

'calculate Qg and BHP needed
If MatrixShrink = "NO" Then
Qg_actual = ((arraytestKrg(i - 1) * Perm_i * h_i * (Psqr - BsqrActual)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000)
Else: Qg_actual = ((arraytestKrg(i - 1) * (Perm_Rat * Perm_i) * h_i * (Psqr - BsqrActual)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000)
End If
If Qg_actual > gasconst_i Then
minbhp = Pwf_i
maxbhp = p
QG = -999
Do Until Abs(QG - gasconst_i) < 0.00001 * gasconst_i
thisbhp = (minbhp + maxbhp) / 2
PrBHP = ((thisbhp)) / (Pc_i + 14.6959488)
AsqrBHP = 0.42747 * (PrBHP / (Tr ^ 2.5))
bBHP = 0.08664 * (PrBHP / Tr)
rBHP = AsqrBHP * bBHP
qBHP = (bBHP ^ 2) + bBHP - AsqrBHP
If Ztype = "Redlich-Kwong" Then
ZBHP = mcroot(1, -1, -qBHP, -rBHP)
ElseIf Ztype = "Brill and Beggs Model" Then
ZBHP = (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101) + ((1 - (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101)) / (Exp((0.62 - 0.23 * Tr) * PrBHP + ((0.066) / (Tr - 0.86) - 0.037) * (PrBHP ^ 2)))) + ((0.132 - 0.32 * WorksheetFunction.Log10(Tr)) * (PrBHP ^ (10 ^ (0.3106 - 0.49 * Tr + 0.1824 * (Tr ^ 2)))))
End If
densityBHP = (0.0433 * (thisbhp) * SGCH4_i / (ZBHP * (Temp_i + 460)))
miuBHP = (k * 10 ^ -4) * Exp(x * (densityBHP ^ y))
Bsqr = ((thisbhp) ^ 2) / (ZBHP * miuBHP)
arraytestBsqr(i) = BsqrBHP

If MatrixShrink = "NO" Then
QG = (arraytestKrg(i - 1) * Perm_i * h_i * (Psqr - Bsqr)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000
Else: QG = (arraytestKrg(i - 1) * (Perm_Rat * Perm_i) * h_i * (Psqr - Bsqr)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000
End If
If QG > gasconst_i Then
minbhp = thisbhp
Else
maxbhp = thisbhp
End If
Loop

ElseIf Qg_actual < gasconst_i Then
QG = Qg_actual
End If



'identifying the MaxBHP and recalculate the Qg and Qw
maximumBHP = WorksheetFunction.Max(thisbhp, BHP)
arraytestBHP(i) = maximumBHP
If BHP < maximumBHP Then
If MatrixShrink = "NO" Then
Qw_new = (arraytestKrw(i - 1) * Perm_i * h_i * (p - maximumBHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))
QW = Qw_new
Else: Qw_new = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - maximumBHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 3 / 4 + skin_i))
QW = Qw_new
End If
ElseIf BHP > maximumBHP Then
End If
If thisbhp < maximumBHP Then
PrBHP_2 = ((maximumBHP)) / (Pc_i + 14.6959488)
AsqrBHP_2 = 0.42747 * (PrBHP_2 / (Tr ^ 2.5))
bBHP_2 = 0.08664 * (PrBHP_2 / Tr)
rBHP_2 = AsqrBHP_2 * bBHP_2
qBHP_2 = (bBHP_2 ^ 2) + bBHP_2 - AsqrBHP_2
If Ztype = "Redlich-Kwong" Then
ZBHP_2 = mcroot(1, -1, -qBHP_2, -rBHP_2)
ElseIf Ztype = "Brill and Beggs Model" Then
ZBHP_2 = (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101) + ((1 - (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101)) / (Exp((0.62 - 0.23 * Tr) * PrBHP_2 + ((0.066) / (Tr - 0.86) - 0.037) * (PrBHP_2 ^ 2)))) + ((0.132 - 0.32 * WorksheetFunction.Log10(Tr)) * (PrBHP_2 ^ (10 ^ (0.3106 - 0.49 * Tr + 0.1824 * (Tr ^ 2)))))
End If
densityBHP_2 = (0.0433 * (maximumBHP) * SGCH4_i / (ZBHP_2 * (Temp_i + 460)))
miuBHP_2 = (k * 10 ^ -4) * Exp(x * (densityBHP_2 ^ y))
BsqrBHP_2 = ((maximumBHP) ^ 2) / (ZBHP_2 * miuBHP_2)
arraytestBsqr(i) = BsqrBHP_2
If MatrixShrink = "NO" Then
QG = (arraytestKrg(i - 1) * Perm_i * h_i * (Psqr - BsqrBHP_2)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000
Else: QG = (arraytestKrg(i - 1) * (Perm_Rat * Perm_i) * h_i * (Psqr - BsqrBHP_2)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000
End If
ElseIf thisbhp > maximumBHP Then
End If

'Re-calculate gas water ratio
If maximumBHP > Pwf_i Then
PrMaximumBHP = ((maximumBHP)) / (Pc_i + 14.6959488)
AsqrMaximumBHP = 0.42747 * (PrMaximumBHP / (Tr ^ 2.5))
bMaximumBHP = 0.08664 * (PrMaximumBHP / Tr)
rMaximumBHP = AsqrMaximumBHP * bMaximumBHP
qMaximumBHP = (bMaximumBHP ^ 2) + bMaximumBHP - AsqrMaximumBHP
If Ztype = "Redlich-Kwong" Then
ZMaximumBHP = mcroot(1, -1, -qMaximumBHP, -rMaximumBHP)
ElseIf Ztype = "Brill and Beggs Model" Then
ZMaximumBHP = (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101) + ((1 - (1.39 * (Tr - 0.92) ^ 0.5 - 0.36 * Tr - 0.101)) / (Exp((0.62 - 0.23 * Tr) * PrMaximumBHP + ((0.066) / (Tr - 0.86) - 0.037) * (PrMaximumBHP ^ 2)))) + ((0.132 - 0.32 * WorksheetFunction.Log10(Tr)) * (PrMaximumBHP ^ (10 ^ (0.3106 - 0.49 * Tr + 0.1824 * (Tr ^ 2)))))
End If
densityMaximumBHP = (0.0433 * (maximumBHP) * SGCH4_i / (ZMaximumBHP * (Temp_i + 460)))
miuMaximumBHP = (k * 10 ^ -4) * Exp(x * (densityMaximumBHP ^ y))
BsqrMaximumBHP = ((maximumBHP) ^ 2) / (ZMaximumBHP * miuMaximumBHP)
arraytestBsqr(i) = BsqrMaximumBHP
If MatrixShrink = "NO" Then
Gas_2_produce = (arraytestKrg(i - 1) * Perm_i * h_i * (Psqr - BsqrMaximumBHP)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * Perm_i * h_i * (p - maximumBHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
Else: Gas_2_produce = (arraytestKrg(i - 1) * (Perm_Rat * Perm_i) * h_i * (Psqr - BsqrMaximumBHP)) / (1422 * (Temp_i + 460) * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 1000 * Bg
Water_2_produce = (arraytestKrw(i - 1) * (Perm_Rat * Perm_i) * h_i * (p - maximumBHP)) / (141.2 * Miuwater_i * Bw * (WorksheetFunction.Ln(re_i / rw_i) - 0.75 + skin_i)) * 5.61458333333333 * Bw
End If
If Gas_2_produce + Water_2_produce = 0 Then
Gas2Water = arraytestGas2Water(i - 1)
Water2gas = arraytestWater2Gas(i - 1)
Else: Gas2Water = (Gas_2_produce) / (Gas_2_produce + Water_2_produce)
Water2gas = (Water_2_produce) / (Gas_2_produce + Water_2_produce)
End If

'Re-calculate cum water and cum gas
water_need_Rcf = (TotalFluid - CleVol) * Water2gas
water_need_Scf = water_need_Rcf / Bw_i
CumWaterRcf = water_need_Rcf + arraytestCumWaterRcf(i - 1)
CumWaterScf = water_need_Scf + arraytestCumWaterScf(i - 1)
gas_need_Rcf = (TotalFluid - CleVol) * Gas2Water
gas_need_Scf = gas_need_Rcf / Bg
CumGasRcf = gas_need_Rcf + arraytestCumGasRcf(i - 1)
CumGasScf = gas_need_Scf + arraytestCumGasScf(i - 1)

'Re-calculate saturations & permeability
If MatrixShrink = "NO" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
Else
If PalMan_i = "YES" Then
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cfnew_i * (Pi_i - p)) * 100))
Else
Sw = WorksheetFunction.Min(100, ((Swi_i * (1 + cw * (Pi_i - p)) + ((5.61458333333333 * We_i - CumWaterRcf) / ((Poro_i / 100) * (areaft2) * h_i))) / (1 - cf_i * (Pi_i - p)) * 100))
End If
End If
If corey_i = "YES" Then
If (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i < 0 Then
krw = 0
Else: krw = (((Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100)) ^ nw_i) * krw0_i
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
ElseIf ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i > krg0_i Then
krg = krg0_i
Else: krg = ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100 - Sgc_i / 100)) ^ ng_i * krg0_i
End If
ElseIf honarpour_i = "YES" Then
If 0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * (Sw / 100) ^ 3.6) * ((Sw / 100 - Swc_i / 100)) < 0 Then
krw = 0
Else: krw = 0.035388 * (Sw / 100 - Swc_i / 100) / (1 - Swc_i / 100) - 0.010874 * ((Sw / 100) / (1 - Swc_i / 100)) ^ 2.9 + (0.56556 * (Sw / 100) ^ 3.6) * ((Sw / 100 - Swc_i / 100))
End If
Sg = 100 - Sw
If Sg <= Sgc_i Then
krg = 0
Else
If krg > 1 Then
krg = 1
Else: krg = 1.1072 * ((Sg / 100 - Sgc_i / 100) / (1 - Swc_i / 100)) ^ 2
End If
End If
End If

ElseIf maximumBHP < Pwf_i Then
End If

'calculate time taken in days
If QW * 5.61458333333333 = 0 Then
time_water = 0
Else: time_water = (water_need_Scf / (QW * 5.61458333333333))
End If
If QG = 0 Then
time_gas = 0
Else: time_gas = (gas_need_Scf / QG)
End If
Days_stream = arraytestDays(i - 1) + time_water
deltime = Days_stream - arraytestDays(i - 1)


'updating arraytest values
If QG > maxQg Then
maxQg = QG
ElseIf QG < maxQg And QG < Qg_abd Then
Exit For
End If
If Days_stream > Day_abd Then
Exit For
End If
arraytestBw(i) = Bw
arraytesttotalgas(i) = totalgas
arraytestGasVolInCleats_scf(i) = GasVolInCleats_scf
arraytestResPres(i) = WorksheetFunction.Max(Pwf_i, p)
arraytestZ(i) = Z
arraytestGC_des_Scf(i) = GC_des_Scf
arraytestGC_des_Rcf(i) = GC_des_Rcf
arraytestmiu(i) = miu
arraytestcw(i) = cw
arraytestPsqr(i) = Psqr
arraytestSw(i) = Sw
arraytestSg(i) = Sg
arraytestGas_2_produce(i) = Gas_2_produce
arraytestWater_2_produce(i) = Water_2_produce

If MatrixShrink = "YES" And ratio = "YES" Then
arraytestPor_Rat(i) = Por_Rat
arraytestPerm_Rat(i) = Perm_Rat
arraytestNewPoro(i) = Por_Rat * (Poro_i / 100)
arraytestNewPerm(i) = Perm_Rat * Perm_i
End If
arraytestTotalFluid(i) = TotalFluid
arraytestwater_need_Scf(i) = water_need_Scf
arraytestgas_need_Scf(i) = gas_need_Scf
arraytestQw(i) = QW
arraytestQw_scf(i) = QW * 5.61458333333333
arraytesttime_water(i) = time_water
arraytesttime_gas(i) = time_gas
arraytestCleVol(i) = CleVol
arraytestQg(i) = QG
arraytestGC(i) = GC
arraytestCumGasDesScf(i) = CumGasDesScf
arraytestCumGasDesRcf(i) = CumGasDesRcf
arraytestBg(i) = Bg
arraytestKrw(i) = krw
arraytestKrg(i) = krg
arraytestGas2Water(i) = Gas2Water
arraytestWater2Gas(i) = Water2gas
arraytestWaterVolInCleats(i) = WaterVolInCleats
arraytestGasVolInCleats(i) = GasVolInCleats
arraytestWaterNeed2Produce(i) = water_need_Rcf
arraytestCumWaterRcf(i) = CumWaterRcf
arraytestCumWaterScf(i) = CumWaterScf
arraytestGasNeed2Produce(i) = gas_need_Rcf
arraytestCumGasRcf(i) = CumGasRcf
arraytestCumGasScf(i) = CumGasScf
arraytestDays(i) = Days_stream
Next
'Outcome based on type of data output choosed
For k = 1 To 30000
If Outputtype = "Field" Then
arraytestResPres_output(k - 1) = arraytestResPres(k - 1)
arraytestDays_output(k - 1) = arraytestDays(k - 1)
arraytestQg_output(k - 1) = arraytestQg(k - 1) / 1000#
arraytestQw_output(k - 1) = arraytestQw(k - 1)
arraytestCumGas_output(k - 1) = arraytestCumGasScf(k - 1)
arraytestCumWater_output(k - 1) = arraytestCumWaterScf(k - 1) * 0.178107607
arraytestGC_output(k - 1) = arraytestGC(k - 1)
QG_title = "Qg(Mscf/day)"
QW_title = "Qw(bbl/day)"
CUMGASPROD_title = "CumGasProd(scf)"
CUMWATERPROD_title = "CumWaterProd(bbls)"
PRESSURE_title = "Pressure(psi)"
DAYS_title = "Days on stream"
gc_unit = "scf/ton"
pres_unit = "psi"
arraytestBHP_output(k - 1) = arraytestBHP(k - 1)
Pwf_title = "Pwf(psi)"
gas_unit = "scf"
water_unit = "bbls"
Else:
arraytestResPres_output(k - 1) = arraytestResPres(k - 1) / 0.14503773801
arraytestDays_output(k - 1) = arraytestDays(k - 1)
arraytestQg_output(k - 1) = arraytestQg(k - 1) * 0.0283168466
arraytestQw_output(k - 1) = arraytestQw_scf(k - 1) * 0.0283168466
arraytestCumGas_output(k - 1) = arraytestCumGasScf(k - 1) * 0.0283168466
arraytestCumWater_output(k - 1) = arraytestCumWaterScf(k - 1) * 0.0283168466
arraytestGC_output(k - 1) = arraytestGC(k - 1) / 32.03798627
QG_title = "Qg(scm/day)"
QW_title = "Qw(scm/day)"
CUMGASPROD_title = "CumGasProd(scm)"
CUMWATERPROD_title = "CumWaterProd(scm)"
PRESSURE_title = "Pressure(kPa)"
DAYS_title = "Days on Stream"
gc_unit = "scc/g"
pres_unit = "kPa"
Pwf_title = "Pwf(kPa)"
arraytestBHP_output(k - 1) = arraytestBHP(k - 1) / 0.14503773801
gas_unit = "scm"
water_unit = "scm"
End If
Next

'Choose data based on outcome form
SelectedDaysarraytest = arraytestSelectedDays(34997)
previousSelectedValue = arraytestDays_output(0)
arraytesttestDays(0) = arraytestDays_output(0)
arraytesttestQg(0) = arraytestQg_output(0)
arraytesttestQw(0) = arraytestQw_output(0)
arraytesttestCumGas(0) = arraytestCumGas_output(0)
arraytesttestCumWater(0) = arraytestCumWater_output(0)
arraytesttestResPres(0) = arraytestResPres_output(0)
arraytesttestSw(0) = arraytestSw(0)
arraytesttestSg(0) = arraytestSg(0)
arraytesttestKrw(0) = arraytestKrw(0)
arraytesttestKrg(0) = arraytestKrg(0)
arraytesttestGC(0) = arraytestGC_output(0)
arraytesttestBHP(0) = arraytestBHP_output(0)
arraytesttestPorRat(0) = arraytestPor_Rat(0)
arraytesttestPermRat(0) = arraytestPerm_Rat(0)
arraytesttestDays(1) = arraytestDays_output(1)
arraytesttestQg(1) = arraytestQg_output(1)
arraytesttestQw(1) = arraytestQw_output(1)
arraytesttestCumGas(1) = arraytestCumGas_output(1)
arraytesttestCumWater(1) = arraytestCumWater_output(1)
arraytesttestResPres(1) = arraytestResPres_output(1)
arraytesttestSw(1) = arraytestSw(1)
arraytesttestSg(1) = arraytestSg(1)
arraytesttestKrw(1) = arraytestKrw(1)
arraytesttestKrg(1) = arraytestKrg(1)
arraytesttestGC(1) = arraytestGC_output(1)
arraytesttestBHP(1) = arraytestBHP_output(1)
arraytesttestPorRat(1) = arraytestPor_Rat(1)
arraytesttestPermRat(1) = arraytestPerm_Rat(1)
averqw = arraytestQw_output(0)
averqg = arraytestQg_output(0)
avercumgas = arraytestCumGas_output(0)
avercumwater = arraytestCumWater_output(0)
averrespres = arraytestResPres_output(0)
aversw = arraytestSw(0)
aversg = arraytestSg(0)
averkrw = arraytestKrw(0)
averkrg = arraytestKrg(0)
averbhp = arraytestBHP_output(0)
averpor = arraytestPor_Rat(0)
averperm = arraytestPerm_Rat(0)

'Data been averaged based on the way of the outcome to be viewed
If Form_of_Outcome = "As it is" Then
y2 = 1
For p2 = 1 To 34997
If arraytestDays_output(p2 - 1) = arraytestDays_output(p2) Then
Exit For
End If
If arraytestQg_output(p2 - 1) < 0 Then
Exit For
End If
arraytesttestDays(y2 - 1) = arraytestDays_output(p2 - 1)
arraytesttestQg(y2 - 1) = arraytestQg_output(p2 - 1)
arraytesttestQw(y2 - 1) = arraytestQw_output(p2 - 1)
arraytesttestCumGas(y2 - 1) = arraytestCumGas_output(p2 - 1)
arraytesttestCumWater(y2 - 1) = arraytestCumWater_output(p2 - 1)
arraytesttestResPres(y2 - 1) = arraytestResPres_output(p2 - 1)
arraytesttestSw(y2 - 1) = arraytestSw(p2 - 1)
arraytesttestSg(y2 - 1) = arraytestSg(p2 - 1)
arraytesttestKrw(y2 - 1) = arraytestKrw(p2 - 1)
arraytesttestKrg(y2 - 1) = arraytestKrg(p2 - 1)
arraytesttestBHP(y2 - 1) = arraytestBHP_output(p2 - 1)
arraytesttestPorRat(y2 - 1) = arraytestPor_Rat(p2 - 1)
arraytesttestPermRat(y2 - 1) = arraytestPerm_Rat(p2 - 1)
y2 = y2 + 1
Next
Else
y2 = 2
y4 = 1
For p2 = 2 To 34996
If arraytestDays_output(p2) - arraytestDays_output(p2 - y4) >= 0.999999 * Days_Step Then
arraytestSelectedDays(y2) = p2
arraytesttestDays(y2) = arraytestDays_output(p2)
averqw = (testqw + arraytestQw_output(p2)) / y4
averpor = (testpor + arraytestPor_Rat(p2)) / y4
averperm = (testperm + arraytestPerm_Rat(p2)) / y4
averqg = (testqg + arraytestQg_output(p2)) / y4
averbhp = (testbhp + arraytestBHP_output(p2)) / y4
avercumgas = (testcumgas + arraytestCumGas_output(p2)) / y4
avercumwater = (testcumwater + arraytestCumWater_output(p2)) / y4
averrespres = (testrespres + arraytestResPres_output(p2)) / y4
aversw = (testsw + arraytestSw(p2)) / y4
aversg = (testsg + arraytestSg(p2)) / y4
averkrw = (testkrw + arraytestKrw(p2)) / y4
averkrg = (testkrg + arraytestKrg(p2)) / y4
arraytesttestQw(y2) = averqw
arraytesttestQg(y2) = averqg
arraytesttestCumGas(y2) = avercumgas
arraytesttestCumWater(y2) = avercumwater
arraytesttestResPres(y2) = averrespres
arraytesttestSw(y2) = aversw
arraytesttestSg(y2) = aversg
arraytesttestKrw(y2) = averkrw
arraytesttestKrg(y2) = averkrg
arraytesttestBHP(y2) = averbhp
arraytesttestPorRat(y2) = averpor
arraytesttestPermRat(y2) = averperm
testpor = 0
testperm = 0
testbhp = 0
testqw = 0
testqg = 0
testcumgas = 0
testcumwater = 0
testrespres = 0
testrespres = 0
testsw = 0
testsg = 0
testkrw = 0
testkrg = 0
y2 = y2 + 1
y4 = 1
Else
testpor = arraytestPor_Rat(p2) + testpor
testperm = arraytestPerm_Rat(p2) + testperm
testbhp = arraytestBHP_output(p2) + testbhp
testqw = arraytestQw_output(p2) + testqw
testqg = arraytestQg_output(p2) + testqg
testcumgas = arraytestCumGas_output(p2) + testcumgas
testcumwater = arraytestCumWater_output(p2) + testcumwater
testsw = arraytestSw(p2) + testsw
testrespres = arraytestResPres_output(p2) + testrespres
testsg = arraytestSg(p2) + testsg
testkrw = arraytestKrw(p2) + testkrw
testkrg = arraytestKrg(p2) + testkrg
y4 = y4 + 1
End If

Next
End If
'convert the input data for the user
convert_output

'display the input for the simulation
Sheets("RiskAnalysis").Range("q5").Offset(0, count - 1) = "Simulation " + CStr(count)
Sheets("RiskAnalysis").Range("q6").Offset(0, count - 1) = Temp_i
Sheets("RiskAnalysis").Range("q7").Offset(0, count - 1) = Pi_i
Sheets("RiskAnalysis").Range("q8").Offset(0, count - 1) = area_i
Sheets("RiskAnalysis").Range("q9").Offset(0, count - 1) = h_i
Sheets("RiskAnalysis").Range("q10").Offset(0, count - 1) = Poro_i
Sheets("RiskAnalysis").Range("q11").Offset(0, count - 1) = Swi_i
Sheets("RiskAnalysis").Range("q12").Offset(0, count - 1) = GC_i
Sheets("RiskAnalysis").Range("q13").Offset(0, count - 1) = bulkdensity_i
Sheets("RiskAnalysis").Range("q14").Offset(0, count - 1) = Perm_i
Sheets("RiskAnalysis").Range("q15").Offset(0, count - 1) = Pwf_i
Sheets("RiskAnalysis").Range("q16").Offset(0, count - 1) = VL_i
Sheets("RiskAnalysis").Range("q17").Offset(0, count - 1) = PL_i
Sheets("RiskAnalysis").Range("q18").Offset(0, count - 1) = skin_i

'display production data
If outcome_2 = "YES" Then
output_results
End If

If plot_2 = "YES" Then
If count = 1 Then
Sheets("RiskOutcome").Select
With ActiveChart
For n = .SeriesCollection.count To 1 Step -1
.SeriesCollection(n).Delete
Next n
End With
End If
Test_output
End If

If outcome_2 = "YES" And plot_2 = "YES" Then
Exit Sub
End If
Set E = Nothing
Set A_Mix = Nothing
Set B_H2S = Nothing
Set CO2 = Nothing
Set H2S = Nothing
Set testpor = Nothing
Set testperm = Nothing
Set testbhp = Nothing
Set testqw = Nothing
Set testqg = Nothing
Set testcumgas = Nothing
Set testcumwater = Nothing
Set testrespres = Nothing
Set testrespres = Nothing
Set testsw = Nothing
Set testsg = Nothing
Set testkrw = Nothing
Set testkrg = Nothing
Set Qg_abd = Nothing
Set Day_abd = Nothing
Set ZMatBal = Nothing
Set peak_gas = Nothing
Set a_i = Nothing
Set w_i = Nothing
Set bulkdensity_i = Nothing
Set Poro_i = Nothing
Set mCH4_i = Nothing
Set mAir_i = Nothing
Set SGCH4_i = Nothing
Set Tc_i = Nothing
Set Pc_i = Nothing
Set We_i = Nothing
Set wt_i = Nothing
Set Swc_i = Nothing
Set nw_i = Nothing
Set krw0_i = Nothing
Set Sgc_i = Nothing
Set ng_i = Nothing
Set krg0_i = Nothing
Set el_i = Nothing
Set KdivideM_i = Nothing
'skin_i = Nothing
Set Swi_i = Nothing
Set Bw_i = Nothing
Set Miuwater_i = Nothing
Set VL_i = Nothing
Set PL_i = Nothing
Set Pi_i = Nothing
Set PKing_i = Nothing
Set Pwf_i = Nothing
Set delP_i = Nothing
Set GC_i = Nothing
Set area_i = Nothing
Set h_i = Nothing
Set Temp_i = Nothing
Set cw_i = Nothing
Set cf_i = Nothing
Set Perm_i = Nothing
Set rw_i = Nothing
Set gasconst_i = Nothing
Set waterconst_i = Nothing
Set Pabd_i = Nothing
Set cfnew_i = Nothing
Set areaft2 = Nothing
Set ResVol_i = Nothing
Set coalweight_i = Nothing
Set Zpwf = Nothing
Set miupwf = Nothing
Set WIIP_i = Nothing
Set re_i = Nothing
Set SeidleHuitt = Nothing
Set ShiDurucan = Nothing
Set kincline = Nothing
Set cm_Seidle = Nothing
Set n_Seidle = Nothing
Set n_PalMan = Nothing
Set MatrixShrink = Nothing
Set v_shi = Nothing
Set E_shi = Nothing
Set el_shi = Nothing
Set sigma_shi = Nothing
Set Pe_shi = Nothing
Set I_kincline = Nothing
Set delsigma = Nothing
Set Por_Rat = Nothing
Set Perm_Rat = Nothing
Set CleVol = Nothing
Set WaterVolInCleats = Nothing
Set GasVolInCleats = Nothing
Set GasVolInCleats_scf = Nothing
Set TotalFluid = Nothing
Set water_need_Rcf = Nothing
Set water_need_Scf = Nothing
Set CumWaterRcf = Nothing
Set CumWaterScf = Nothing
Set gas_need_Rcf = Nothing
Set gas_need_Scf = Nothing
Set CumGasRcf = Nothing
Set CumGasScf = Nothing
Set Pr = Nothing
Set Tr = Nothing
Set Asqr = Nothing
Set b = Nothing
Set r = Nothing
Set q = Nothing
Set Z = Nothing
Set f = Nothing
Set g = Nothing
Set k = Nothing
Set x = Nothing
Set y = Nothing
Set density = Nothing
Set miu = Nothing
Set Bg = Nothing
Set Psqr = Nothing
Set Bsqr = Nothing
Set krg = Nothing
Set cw = Nothing
Set Sw = Nothing
Set Sg = Nothing
Set krw = Nothing
Set QW = Nothing
Set BHP = Nothing
Set GC = Nothing
Set GC_des_Scf = Nothing
Set GC_des_Rcf = Nothing
Set CumGasDesRcf = Nothing
Set CumGasDesScf = Nothing
Set time_water = Nothing
Set time_gas = Nothing
Set Days_stream = Nothing
Set Gas_2_produce = Nothing
Set Water_2_produce = Nothing
Set Gas2Water = Nothing
Set Water2gas = Nothing
Set PrBHP = Nothing
Set AsqrBHP = Nothing
Set bBHP = Nothing
Set rBHP = Nothing
Set qBHP = Nothing
Set ZBHP = Nothing
Set fBHP = Nothing
Set gBHP = Nothing
Set CBHP = Nothing
Set DBHP = Nothing
Set EBHP = Nothing
Set kBHP = Nothing
Set xBHP = Nothing
Set yBHP = Nothing
Set densityBHP = Nothing
Set miuBHP = Nothing
Set PrBHP_Qw = Nothing
Set AsqrBHP_Qw = Nothing
Set bBHP_Qw = Nothing
Set rBHP_Qw = Nothing
Set qBHP_Qw = Nothing
Set ZBHP_Qw = Nothing
Set densityBHP_Qw = Nothing
Set miuBHP_Qw = Nothing
Set BsqrBHP_Qw = Nothing
Set maximumBHP = Nothing
Set QG = Nothing
Set QgActual = Nothing
Set BsqrActual = Nothing
Set Qw_new = Nothing
Set PrBHP_new = Nothing
Set AsqrBHP_new = Nothing
Set bBHP_new = Nothing
Set rBHP_new = Nothing
Set qBHP_new = Nothing
Set ZBHP_new = Nothing
Set fBHP_new = Nothing
Set gBHP_new = Nothing
Set CBHP_new = Nothing
Set DBHP_new = Nothing
Set EBHP_new = Nothing
Set kBHP_new = Nothing
Set xBHP_new = Nothing
Set yBHP_new = Nothing
Set densityBHP_new = Nothing
Set miuBHP_new = Nothing
Set Qg_new = Nothing
Set PrMaximumBHP = Nothing
Set AsqrMaximumBHP = Nothing
Set bMaximumBHP = Nothing
Set rMaximumBHP = Nothing
Set qMaximumBHP = Nothing
Set ZMaximumBHP = Nothing
Set densityMaximumBHP = Nothing
Set miuMaximumBHP = Nothing
Set BsqrMaximumBHP = Nothing
Set PrBHP_2 = Nothing
Set AsqrBHP_2 = Nothing
Set bBHP_2 = Nothing
Set rBHP_2 = Nothing
Set qBHP_2 = Nothing
Set ZBHP_2 = Nothing
Set densityBHP_2 = Nothing
Set miuBHP_2 = Nothing
Set BsqrBHP = Nothing
Set p = Nothing
Set delPresNew = Nothing
Set Pr_initial = Nothing
Set Tr_initial = Nothing
Set Asqr_initial = Nothing
Set b_initial = Nothing
Set r_initial = Nothing
Set q_initial = Nothing
Set Z_initial = Nothing
Set Bg_initial = Nothing
Set totalgas = Nothing
Set PalMan_i = Nothing
Set Tr = Nothing
Set k = Nothing
Set x = Nothing
Set y = Nothing
Set GCabd_i = Nothing
Set Recover_GC_Iso = Nothing
Set RF_Iso = Nothing
Set Pres_Iso = Nothing
Set GC_Iso = Nothing
Set LangIso_i = Nothing
Set RelPerm_i = Nothing
Set outcome_i = Nothing
Set Sw_i = Nothing
Set krw_i = Nothing
Set krwcorrect_i = Nothing
Set Sg_i = Nothing
Set krg_i = Nothing
Set krgcorrect_i = Nothing
Set check_GC = Nothing
Set outcomeform = Nothing
Erase arraytestCH4density
Erase arraytestZMatBal
Erase arraytesttestBHP
Erase arraytestBHP_output
Erase arraytestGC
Erase arraytestinitial_gas
Erase arraytestSelectedDays
Erase arraytestPres_Iso
Erase arraytestGC_Iso
Erase arraytestdelPres
Erase arraytestResPres
Erase arraytestCumGasDesRcf
Erase arraytestCumGasDesScf
Erase arraytestBg
Erase arraytestKrw
Erase arraytestKrg
Erase arraytestGas2Water
Erase arraytestWater2Gas
Erase arraytestWaterVolInCleats
Erase arraytestGasVolInCleats
Erase arraytestGasVolInCleats_scf
Erase arraytestWaterNeed2Produce
Erase arraytestWaterNeed2Produce_Scf
Erase arraytestCumWaterRcf
Erase arraytestCumWaterScf
Erase arraytestGasNeed2Produce
Erase arraytestGasNeed2Produce_Scf
Erase arraytestCumGasRcf
Erase arraytestCumGasScf
Erase arraytestDays
Erase arraytestZ
Erase arraytestGC_des_Scf
Erase arraytestGC_des_Rcf
Erase arraytestmiu
Erase arraytestcw
Erase arraytestPsqr
Erase arraytestSw
Erase arraytestSg
Erase arraytestGas_2_produce
Erase arraytestWater_2_produce
Erase arraytestPor_Rat
Erase arraytestNewPoro
Erase arraytestPerm_Rat
Erase arraytestNewPerm
Erase arraytestTotalFluid
Erase arraytestwater_need_Scf
Erase arraytestgas_need_Scf
Erase arraytestQw
Erase arraytestQw_scf
Erase arraytestBHP
Erase arraytesttime_water
Erase arraytesttime_gas
Erase arraytestDays_stream
Erase arraytestCleVol
Erase arraytestQg
Erase arraytestBsqr
Erase arraytesttotalgas
Erase arraytestResPres_output
Erase arraytestDays_output
Erase arraytestQg_output
Erase arraytestQw_output
Erase arraytestCumGas_output
Erase arraytestCumWater_output
Erase arraytestSw_i
Erase arraytestSg_i
Erase arraytestKrw_act
Erase arraytestKrw_i
Erase arraytestKrg_act
Erase arraytestKrg_i
Erase arraytesttestDays
Erase arraytesttestQg
Erase arraytesttestQw
Erase arraytesttestCumGas
Erase arraytesttestCumWater
Erase arraytesttestResPres
Erase arraytesttestSw
Erase arraytesttestSg
Erase arraytesttestKrw
Erase arraytesttestKrg
Erase arraytesttestGC
Erase arraytestGC_output
Erase arraytestPorRat_output
Erase arraytesttestPorRat
Erase arraytestPermRat_output
Erase arraytesttestPermRat
Set QG_title = Nothing
Set QW_title = Nothing
Set PRESSURE_title = Nothing
Set DAYS_title = Nothing
Set CUMGASPROD_title = Nothing
Set CUMWATERPROD_title = Nothing
Next

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
the mean, standard deviation, maximum, minimum and also mode values are key in by user by using *Inputbox* earlier on..Please do ask me if you need more explanation on this macro..Sorry for the mess as i am still learning..
 
Upvote 0

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