Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:00:52+00:00 2026-06-18T02:00:52+00:00

My user defined function always returns ‘A value used in the formula is of

  • 0

My user defined function always returns ‘A value used in the formula is of the wrong data type’ (I am passing cell ranges to the function). I’ve searched lots of threads and am quite sure my code should work:

Function SortAndEvaluate(ByRef Probs() As Variant, ByRef ResidualProbs() As Variant, ByRef Costs() As Variant)
    Dim Temp As Double
    Dim i As Integer
    Dim NoExcanges As Integer


    'Exchange values in probs in descending order

    Do
        NoExchanges = True
        ' Loop through each element
        For i = 0 To UBound(Probs) - 1

            'If element is greater than the last exchange, else do nothing

            If Probs(i) > Probs(i + 1) Then
                NoExchanges = False
                ' Exchange probability values
                Temp = Probs(i)
                Probs(i) = Probs(i + 1)
                Probs(i + 1) = Temp
                ' Exchange residual probability values
                Temp = ResidualProbs(i)
                ResidualProbs(i) = ResidualProbs(i + 1)
                ResidualProbs(i + 1) = Temp
                ' Exchange cost values
                Temp = Costs(i)
                Costs(i) = Costs(i + 1)
                Costs(i + 1) = Temp
            End If
        Next i
    Loop While Not (NoExchanges)

    Temp = 0
    For i = 1 To UBound(Probs)
        If i = 1 Then
            Temp = Temp + Probs(i) * Costs(i)
        Else
            Temp = Temp + Probs(i) * ResidualProbs(i - 1) * Costs(i)
        End If
    Next i

    SortAndEvaluate = Temp
End Function

Can anyone give me any feedback please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:00:53+00:00Added an answer on June 18, 2026 at 2:00 am

    Try something like

    Option Explicit
    
    Function SortAndEvaluate(ByRef Probs As Range, ByRef ResidualProbs As Range, ByRef Costs As Range)
    
        Dim NumberOfProbs As Long
        Dim NumberOfProbsRows As Long
        Dim NumberOfProbsColumns As Long
        Dim RangeAddressProbs As String
    
        ' Dim for ResidualProbs and Costs
        ' ...
    
        NumberOfProbs = Probs.Cells.Count
        NumberOfProbsRows = Probs.Rows.Count
        NumberOfProbsColumns = Probs.Columns.Count
        RangeAddressProbs = Probs.Address
    
        ' also for ResidualProbs and Costs
        ' ...
    
        ' Might want some validation, e.g.
        If NumberOfProbsColumns <> 1 Then Exit Function
        If NumberOfResidualProbsColumns <> 1 Then Exit Function
    
        ' Get Values from range as an array
        Dim ValuesProbs As Variant
        Dim ValuesResidualProbs As Variant
        Dim ValuesCosts As Variant
    
        ValuesProbs = Probs
        ValuesResidualProbs = ResidualProbs
        ValuesCosts = Costs
    
        ' Do your logic stuff using the Values... arrays
        ' ...
    
        ' This example return value shows how to reference the Values... arrays (note you need to index the column too)
        SortAndEvaluate = ValuesProbs(NumberOfProbsRows, 1)
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I'm passing a command into a user-defined function in PHP. How do I
Hey there, I want to evaluate a mathematical function (user-defined) which returns several values
I was wondering if a user defined function will always needs a return() statement
User Defined Function (UDFs) are functions that one can program and can be dynamically
I have this user defined function. public partial class UserDefinedFunctions { static int i;
Why calling a user defined function need the owner name when calling a stored
I have a User Defined Function (UDF) written in Java to parse lines in
I'm adding a user defined function to Excel with varargs-based signature in C++: LPXLOPER
I am trying to call a user defined function in jQuery: $(document).ready(function() { $('#btnSun').click(function()
Most systems will have a user-defined function (UDF) available. Some will not. i want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.