Consulting

Results 1 to 10 of 10

Thread: nesting a vba function

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    nesting a vba function

    hello
    i want to create a udf that will sum the number of all letters in a range
    [VBA]
    Function sumlen(r As Range) As Integer
    sumlen = Application.WorksheetFunction.Sum(Len(r))
    End Function

    [/VBA]
    what is wronge with my function?
    thanks
    moshe

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    summing each cell in the range individually.[VBA]Function sumlen(r As Range) As Integer
    Dim cell As Range
    For Each cell In r
    sumlen = sumlen + Application.WorksheetFunction.Sum(Len(cell))
    Next cell
    End Function[/VBA]Charlize

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why not just use an array formula?

    =SUM(LEN(A1:A10))

    Entered with CTRL+SHIFT+ENTER.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    =SUMPRODUCT(--(LEN(A1:A10)))

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Charlize
    summing each cell in the range individually.[vba]Function sumlen(r As Range) As Integer
    Dim cell As Range
    For Each cell In r
    sumlen = sumlen + Application.WorksheetFunction.Sum(Len(cell))
    Next cell
    End Function[/vba]Charlize
    Why bother with the worksheet functions?

    [vba]

    Function sumlen(r As Range) As Integer
    Dim cell As Range
    For Each cell In r
    sumlen = sumlen + Len(cell.Value)
    Next cell
    End Function
    [/vba]

  6. #6
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    It was a very, very quick and dirty answer.

  7. #7
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    i have a table with two columns .price & quantity of various goods.i want to get the overall value of the goods supplied.
    [VBA]
    Function turnover(price As Long, quantity As Integer)
    Application.Volatile
    turnover = Application.WorksheetFunction.SumProduct(price, quantity)
    End Function
    [/VBA]
    why can't i use the sum product in a udf? .it work pefectly as a formula.
    thanks
    moshe

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because it isn't exposed to the WorksheetFunction method.

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    The Sumproduct function expects ranges as arguments, not just numbers.

    There is one problem with the function in VBA, you can't reallyh use coercion.

    eg using the double -- won't work

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    MsgBox Activesheet.Evaluate(&quot;SUMPRODUCT(--(A1:A10>1),--(A1:A10<10))&quot [/vba]

Posting Permissions

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