Excel 內建函數雖然強大,但無法涵蓋所有專業計算需求。透過 VBA 自訂函數(User Defined Function, UDF),我們能封裝專屬邏輯,像使用內建函數一樣在工作表中直接呼叫。
Function TrapezoidArea(upperBase As Double, lowerBase As Double, height As Double) As Double
TrapezoidArea = height * (upperBase + lowerBase) / 2
End Function
=TrapezoidArea(A2, B2, C2) 使用。預設情況下,自訂函數僅能在建立它的活頁簿中使用。 若需跨檔案共用,建議將巨集儲存於「個人巨集活頁簿」或另存為 增益集(.xlam) 並透過「開發工具 → 增益集」載入。