+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17

Thread: Converting formula to UDF

  1. #16
    Registered User
    Join Date
    05-23-2011
    Location
    Tucson, AZ
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Converting formula to UDF

    So any help converting the original code into a UDF? The function i have provided i believe is close, but it's not coming out correctly... Any help is appreciated

  2. #17
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Converting formula to UDF

    Try this:
    Function MyConvert(sMeas As String, dAmt As Double, nServ As Long) As String
        Dim dExt      As Double     ' extended amount
        Dim nLB       As Long       ' number of lb
        Dim nOz       As Long       ' number of oz
    
        dExt = nServ * dAmt
    
        Select Case LCase(sMeas)
            Case "weight"
                ' assumes dAmt is in oz
                nLB = dExt \ 16
                nOz = dExt - 16 * nLB
                
                If nLB Then MyConvert = Format(nLB, "0 lb")
                If nOz Then MyConvert = LTrim(MyConvert & Format(nOz, " 0 oz"))
    
            Case "volume"
                MyConvert = "???"
    
            Case Else
                MyConvert = "???"
        End Select
    End Function
    E.g.,

          --B--- -C- -----D------ ------------------E------------------
      2    nServ   6                                                   
      3   sMeas  Amt Extended Amt                                      
      4   Weight   3 1 lb 2 oz    D4 and down: =myconvert(B4, C4, $C$2)
      5   Weight   4 1 lb 8 oz                                         
      6   Weight   5 1 lb 14 oz                                        
      7   Weight   6 2 lb 4 oz                                         
      8   Weight   7 2 lb 10 oz                                        
      9   Weight   8 3 lb
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0