2015-09-28 78 views
0

當我嘗試運行此宏,我收到了味精說值分配給一個子產生編譯錯誤:參數不可選

Compile Error: Argument Not optional

,它凸顯了pressureDrop最後的公式所示。

Public Sub pressureDrop(p As Double, L As Double, _ 
         D As Double, u As Double, _ 
         e As Double, w As Double) 

    Dim A As Double, B As Double, f As Double, k As Double 
    Dim Re As Double, Rng1 As Range, Rng2 As Range 

    Re = p * D * u/w 

    With WorksheetFunction 

     A = (2.457) * .Ln(1/(7/Re)^0.9 + (0.27 * e/D)) 
     B = (37530/Re)^16 
     f = 2 * ((8/Re)^12 + 1/(A + B)^(3/2))^(1/12) 

     Set Rng1 = Range("D2:D29") 
     Set Rng2 = Range("E2:E29") 
     k = .SumProduct(Rng1, Rng2) 

    End With 

    pressureDrop = p * ((4 * f * (L/D) + k) * (u^2)/2) 

End Sub 
+2

您正在嘗試編寫'Function',而不是'Sub'。將聲明更改爲'Public Function pressureDrop(...' –

+0

主要問題是您的代碼試圖給Sub()分配一個返回值。返回值的唯一方法是從Function()。但是如果你提供更多的環境,社區將能夠更好地指導您 –

回答

0

您也可以調暗另一個變量和最終值分配給它,而不是分配給它的到pressuredrop

Dim dAnswer as Double 
dAnswer = p * ((4 * f * (L/D) + k) * (u^2)/2) 
0

將其更改爲一個函數:

Public Function pressureDrop(...) 
    ... 

    pressureDrop = p * ((4 * f * (L/D) + k) * (u^2)/2) 
End Function 
0

更換

Public Sub pressureDrop(p As Double, L As Double, _ 
        D As Double, u As Double, _ 
        e As Double, w As Double) 

Public Function pressureDrop(p As Double, L As Double, _ 
        D As Double, u As Double, _ 
        e As Double, w As Double) as Double