+ Reply to Thread
Results 1 to 3 of 3

Creating a UDF

  1. #1
    Registered User
    Join Date
    03-10-2022
    Location
    Illinois
    MS-Off Ver
    Office 365
    Posts
    14

    Creating a UDF

    Greetings all,
    I am trying to create a function that adds up a range numbers based on the value of a different range of cells. The function will be used in a cell in my sheet, and the range that is passed to it is $C4:$O4. Each item in this range contains either a "Yes" or a "No". Based on my research, it seems that the range would best be converted into an array when passed into the Function (but maybe not?), and then the array elements are scanned through to determine the action on it. For each "Yes", I want to accumulate a value contained in row 2 of the same column. The values in Row 2 are decimals that add up to 1.0, each of which is a weight ranking of the categories of all the "Yes" tallies in row 4. "Yes" and "No" is like Pass/Fail. You only get credit of the weighted value if you PASS the item. However, all I get in the cell that houses the Function is "#Value!". I guess this means that I'm not using the Function correctly somehow, because it doesn't seem even be getting into the code of the Function before failing. I had thought this would be simple thing, but I must be doing something incorrectly that is very basic. (NOTE: The column O is a catchall for Auto Failure. If its a YES, then the score is 0% regardless of what the other scores are.)

    Any help getting past the error is much appreciated.

    ' Below should be an image of the sheet. If not, I have attached the entire file. The TEMPLATE sheet is the one I am working on. Column Q row 4 is where the Function is stored.
    Capture.JPG


    This is my function code (UDF).

    Function CalcResult(Rng As Range) As Single
    ' This Function calculates the result score.
    ' This adds up thescore weights for each item that is Not a NO.
    ' If the Autofail column is set to you, the Resutling Score is 0%
    '==========================================
    Dim Score As Single, K As Integer, CurrentRow As Integer
    Dim MyArray() As Variant

    MyArray() = WorksheetFunction.Transpose(WorksheetFunction.Transpose(Range(Rng)))
    'Set MyArray() = Range(Rng)
    Score = 0#

    If MyArray(UBound(MyArray)) <> "Yes" Then ' Autofail is not true
    For K = 1 To UBound(MyArray) ' columns for scores in row 2
    If MyArray(K) <> "No" Then ' add the score value
    ' Accumulate passing score weighted values
    Score = Score + Application.Cells(2, K + 2).Value
    End If
    Next
    End If

    CalcResult = Score

    End Function
    Attached Files Attached Files
    Thank you,
    CSAScott

  2. #2
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: Creating a UDF

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    This formula does the trick but be careful because there is a ALT+ENTER character in de column header name

    Please Login or Register  to view this content.

    It can be made better no doubt but is does what is needed
    Check the 100% fail with a If Formula
    Attached Files Attached Files
    Last edited by Joske920; 03-19-2022 at 08:05 PM.
    Please be as complete as possible in your asking so it may save use all the time to rework the solution because you didn't give all the requirements. If you have a layout in mind please work it out first so we can adapt our solution to it. Thanks.
    If you have been helped, maybe you could click the *

  3. #3
    Registered User
    Join Date
    03-10-2022
    Location
    Illinois
    MS-Off Ver
    Office 365
    Posts
    14

    Re: Creating a UDF

    You guys are amazing. Thank you so much, and I'm so happy I joined this forum.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Difference between manually creating Form and programmatically creating
    By saziz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-10-2015, 01:06 PM
  2. Replies: 1
    Last Post: 05-20-2015, 04:27 PM
  3. Creating dynamic userform based on several file names and creating chart overlays
    By Thorn23 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-18-2014, 12:09 PM
  4. [SOLVED] creating and re-creating 'random' numbers from a specific seed that can be recreated
    By OberonViking in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-15-2014, 04:13 PM
  5. Creating a selector program - help creating the dropdown menus
    By MattP299 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2011, 03:19 PM
  6. help with macro - creating new columns/copying/creating new worksheets from default
    By vsantoro in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-17-2010, 09:29 AM
  7. Creating a Range after Creating a Different One
    By cheaperThanAPro in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2008, 10:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1