Using named cells in a VBA function Using named cells in a VBA function vba vba

Using named cells in a VBA function


If you just write min_w = L_W_1 * 0.868 * depth / 1000 vba thinks L_W_1 it's variable (of the type variant with value=0).You have to do it like this Range("L_W_1").Value to reference the named cell.

It should work if you change it to:

Function min_w(depth As Long)If depth < Range("SUM_LEN_1").Value Then    min_w = Range("L_W_1").Value * 0.868 * depth / 1000Else    min_w = Range("L_W_1").Value * 0.868 * Range("SUM_LEN_1").Value / 1000 + Range("L_W_2").Value * 0.868 * (depth - Range("SUM_LEN_1").Value) / 1000End IfEnd Function


You can just put them in brackets to mark them as a range:[Sum_Len_1].Value, [L_W_2].Value and [L_W_1].Value