Consulting

Results 1 to 13 of 13

Thread: How to Create UDF

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    32
    Location

    How to Create UDF

    Dear Mentors/Friends
    Hope You are doing good
    Though i have watched so many videos and gone though several post to create formula to excel udf ( User Defined Function) that i can use it easily however i am not sure how to prepare it.

    Here is the formula

    E2 =IF(COUNTIF(A22,"Yes"),"Rule"&SUBSTITUTE(TRIM(IF(A2="Yes","1 ","")&IF(B2="Yes","2 ","")&IF(C2="Yes","3 ","")&IF(D2="Yes","4",""))," ",","),"")



    Also is it possible to select the ranges though input box
    Just started learning excel and vba not very much sure how to do it .

    Thanks for you efforts

  2. #2
    Can't answer your first request but this should give you an idea for your InputBox question.


    Sub Select_With_InputBox()
        Dim Rng As Range
        On Error GoTo Canceled
        Set Rng = Application.InputBox(Prompt:="Select a Range with the mouse." & vbLf & "For multiple Ranges, hold the ""Ctrl"" Button down.", Title:="Select Range(s)", Type:=8)
        'Do something like clearing the selection(s)
        Rng.ClearContents
    Canceled:
    End Sub

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by sg2209 View Post
    Hope You are doing good
    I always try to do good when I can, although I wouldn't quite put myself in the category of do-gooders. In fact I can be downright wicked at times.
    I am, however, quite well.
    Quote Originally Posted by sg2209 View Post
    to create formula to excel udf ( User Defined Function) that i can use it easily however i am not sure how to prepare it.
    It would be good if you could supply a workbook to give us a better idea of how you're going to use it.
    Depending on your version of Excel this UDF might do it for you:
    Function WhatRules1(rng1, rng2)
    WhatRules1 = Evaluate("IF(COUNTIF(" & rng1.Address(external:=True) & ",""Yes""),""Rule "" & TEXTJOIN("","",TRUE,FILTER({1,2,3,4}," & rng2.Address(external:=True) & "=""Yes"",""none"")),"""")")
    End Function
    and you'd use it like this:
    2021-02-01_095102.png

    If your version of Excel is older, then the above won't work, but this one might:
    Function WhatRules2(rng1, rng2)
    If Evaluate("COUNTIF(" & rng1.Address(external:=True) & ",""Yes"")") Then
      Count = 0
      For Each cll In rng2.Cells
        Count = Count + 1
        If cll.Value = "Yes" Then wr2 = wr2 & Count & ","
      Next cll
      If Len(wr2) > 0 Then wr2 = "Rules " & Left(wr2, Len(wr2) - 1) Else wr2 = "No rules"
      WhatRules2 = wr2
    Else
      WhatRules2 = "Not Applicable"
    End If
    End Function
    used in exactly the same way as the first UDF (except it's called WhatRules2.
    Both of these UDFs assume a sequence 1,2,3,4, which you may not want. This is why it's better to provide a workbook and a bit more explanation of how you want it to work.
    Quote Originally Posted by sg2209 View Post
    Also is it possible to select the ranges though input box
    All UDFs when used on a worksheet will allow you to select ranges with the mouse or type the references in.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I do not recommend using InputBoxes in UDFs, because, if you use that UDF in many places, you will need to input the Ranges for all cells using that UDF every time the sheet calculates... And there is no easy way to tell which Cell you need to reenter the ranges for.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    I thought that the InputBox question was separate from the UDF question but maybe not.

  6. #6
    VBAX Regular
    Joined
    Jan 2018
    Posts
    32
    Location
    Thank You for the response, appreciate your efforts
    Let me check and will let you know

  7. #7
    Re: "Let me check and will let you know."
    Are you doing this for a customer?

  8. #8
    VBAX Regular
    Joined
    Jan 2018
    Posts
    32
    Location
    No i am using this for my self not for any customer, First thing i will understand the logic to learn something new and i can ask my subboridnate to do it this can reduce my work

  9. #9
    VBAX Regular
    Joined
    Jan 2018
    Posts
    32
    Location

    Added Dummy File

    I tried both the code unfortunately getting an error #Value.

    Added file Dummy here, please review
    Attached Files Attached Files

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    See attached.
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by p45cal View Post
    See attached.
    I'd suggest a couple of changes

    Function WhatRules2(rng1 As Range, rng2 As Range) As Variant
        Dim i As Long
        Dim s As String
        
        WhatRules2 = CVErr(xlErrNA)
    
    
        For i = 1 To rng1.Columns.Count - 1 Step 2
            If rng1.Cells(1, i).Value = "Yes" Then s = s & ", " & rng2.Cells(1, i).Value
        Next i
    
    
        If Len(s) > 0 Then WhatRules2 = Mid(s, 3)
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    VBAX Regular
    Joined
    Jan 2018
    Posts
    32
    Location
    Thank You so much p45cal & Paul_Hossler.

    Worked like a charm, really appreciate your efforts thanks a ton again

    Thank You jolivanes

  13. #13
    VBAX Regular
    Joined
    Jan 2018
    Posts
    32
    Location
    Thank You so much everyone

Tags for this Thread

Posting Permissions

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