0

I'm writing a VBA function which I need in my Excel spreadsheet, this is the code I wrote:

Function costo(x As Variant, d1 As Double, p1 As Double)
    Dim d As Variant
    d = Array(Array(129, 90), Array(129, 98), Array(142, 81), Array(133, 98), _
             Array(139, 102), Array(156, 144), Array(125, 127), Array(137, 222), _
             Array(213, 241), Array(145, 229), Array(206, 118), Array(152, 167))
    Dim c As Double
    c = 0
    Dim i As Long
    For i = 1 To 12
        c = c + 50 * x(i) * distanza(d1, p1, d(i)(0), d(i)(1))
    Next i
    costo = c
End Function


Function distanza(ByVal d1 As Double, ByVal p1 As Double, ByVal d2 As Double, ByVal p2 As Double) As Double
    Dim r As Double
    r = 6371
    distanza = 2 * r * WorksheetFunction.Asin(Sqr(WorksheetFunction.Power(Sin((d1 - d2) / 2), 2) + _
             Cos(d1) * Cos(d2) * WorksheetFunction.Power(Sin((p1 - p2) / 2), 2)))
End Function

x are 12 cells of my spreadsheet, while d1 and p1 are two cell.

When I run the code in debug, the variable costo have the correct value, but in my spreedsheet I get #VALUE!.

Any suggestion?

8
  • 2
    Debug.? lbound(d, 1), ubound(d, 1) Commented Feb 19, 2023 at 19:15
  • Call costo from a Sub, then you can debug it. Commented Feb 19, 2023 at 19:19
  • Your costo function appears to be missing an As value declaration. Commented Feb 19, 2023 at 19:45
  • May be, error occurs in function costo. Most likely pointed by @GSerg. In this case Excel not informs about error. Cell value become #VALUE!. Check errors with on error clause. Commented Feb 19, 2023 at 20:17
  • 1
    I believe the For Loop should be 0 to 11. And depending on how X is defined maybe it is referenced by x(i+1) or remains by x(i) Commented Feb 19, 2023 at 22:06

2 Answers 2

0

Try to add error handler on error go to, exit function and messagebox. I got same #VALUE but i dunno what numbers you adding. My error says subcript out of range. Numbers too high.

Function costo(x As Variant, d1 As Double, p1 As Double)
    On Error GoTo MY_ERROR
    Dim d As Variant
    d = Array(Array(129, 90), Array(129, 98), Array(142, 81), Array(133, 98), _
             Array(139, 102), Array(156, 144), Array(125, 127), Array(137, 222), _
             Array(213, 241), Array(145, 229), Array(206, 118), Array(152, 167))
    Dim c As Double
    c = 0
    Dim i As Long
    For i = 1 To 12
        c = c + 50 * x(i) * distanza(d1, p1, d(i)(0), d(i)(1))
    Next i
    costo = c
    Exit Function
    
MY_ERROR:
    MsgBox Err.Description
End Function
Sign up to request clarification or add additional context in comments.

Comments

0

The bug in your code causing the premature exit occurs because d is a 12-element variant array with Lbound = 0 and Ubound = 11. So when you refer to the array elements in your c= line, when i reaches 12 you will have an out-of-bounds subscript error.

Change that line to:

c = c + 50 * x(i) * distanza(d1, p1, d(i - 1)(0), d(i - 1)(1))

and your code should run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.