VBA Excel GoalSeek有价值

时间:2018-02-17 23:28:52

标签: vba excel-vba excel

fx 等于 0 时,我希望我的代码能够找到 h 的值。因此,我想到了使用GoalSeek方法,但我似乎无法破解它。我知道我可能应该将所有这些操作存储在一个函数中,但我对VBA还是比较新的。请帮忙!

Option Explicit

Dim d As Double, h As Double, k As Double, p As Double
Dim Ag As Double, t As Double, Al As Double
Dim Sg As Double, Sl As Double, Si As Double, Pi As Double
Dim Dl As Double, ul As Double
Dim Dg As Double, ug As Double
Dim Reg As Double, Rel As Double
Dim taug As Double, taul As Double, taui As Double
Dim fg As Double, fl As Double, fi As Double, fx As Double
Dim rl As Double, rg As Double
Dim vsl As Double, vsg As Double
Dim mul As Double, mug As Double
Dim alpha As Double

Sub findhwhenfx=0()

    Pi = 3.14159265358979 'Fixed Value in Worksheet    
    t = Range("B1").Value 'Fixed Value in Worksheet    
    d = Range("B2").Value 'Fixed Value in Worksheet    
    h = Range("D12").Value 'Value to be found once I click run     
    vsl = Range("B4").Value 'Fixed Value in Worksheet    
    vsg = Range("B3").Value 'Fixed Value in Worksheet    
    mul = Range("B6").Value 'Fixed Value in Worksheet    
    mug = Range("B5").Value 'Fixed Value in Worksheet    
    rl = Range("B7").Value 'Fixed Value in Worksheet    
    rg = Range("B8").Value 'Fixed Value in Worksheet    
    alpha = Range("B9").Value 'Fixed Value in Worksheet 

    Range("U12").GoalSeek Goal:=0, ChangingCell:=Range("D12")    
    k = (2 * h) - 1    
    p = Application.Acos(k)    
    Ag = (p - k * Sqr(1 - (k) ^ 2)) * (d) ^ 2 / 4    
    Range("E12").Value = Ag    
    Al = t - Ag    
    Range("F12").Value = Al    
    Sg = d * p    
    Range("G12").Value = Sg    
    Sl = (Pi * d) - Sg    
    Range("H12").Value = Sl    
    Si = d * Sqr(1 - k ^ (2))    
    Range("I12").Value = Si

    Dl = (4 * Al) / Sl    
    Range("J12").Value = Dl    
    ul = (vsl * t) / Al    
    Range("K12").Value = ul    
    Dg = (4 * Ag) / (Sg + Si)    
    Range("L12").Value = Dg    
    ug = (vsg * t) / Ag    
    Range("M12").Value = ug    
    Rel = (rl * Dl * ul) / mul    
    Range("N12").Value = Rel    
    Reg = (rg * Dg * ug) / mug    
    Range("O12").Value = Reg

    fg = 0.046 * (Reg) ^ (-0.2)    
    Range("Q12").Value = fg    
    fl = 0.046 * (Rel) ^ (-0.2)    
    Range("S12").Value = fl    
    taug = (fg * rg * ((ug) ^ 2)) / 2    
    Range("P12").Value = taug    
    taul = (fl * rl * ((ul) ^ 2)) / 2    
    Range("R12").Value = taul    
    taui = (((ug) ^ 2) * rg * fg) / 2    
    Range("T12").Value = taui

    fx = ((taug * Sg) / Ag) - ((taul * Sl) / Al) + ((taui * Si) * ((1 / Al) + (1 / Ag))) + (rl - rg) * 9.8 * Sin(alpha)    
    Range("U12").Value = fx

End Sub

0 个答案:

没有答案