Results 1 to 3 of 3

Thread: [RESOLVED] simple vba function

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    27

    Resolved [RESOLVED] simple vba function

    Hi guys,

    i am fine with sub routines, first time i am trying a function.


    Function Value1(ByRef p As Range, ByRef d As Range) As Variant


    If p.Value = "" Then p.Value = 0

    (**if i but "end if" here i get a block if error)

    If d.Value = "" Then d.Value = 0

    (**if i but "end if" here i get a block if error)


    Value1 = p.Value + d.Value


    End Function


    Bascially based on whether or not a number is present (there is a formula in the cell to determine whether or not a number will be in a cell), i want to it to sum each of the values of the two ranges. If one of the two cells is empty, i want it to assign 0. I keep on getting a value error if one of the values is empty.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: simple vba function

    If p.Value = "" Then p.Value = 0
    this tries to change the value of the range, which you can not do within the function, try like
    Code:
    Function value1(p As Range, d As Range)
    value1 = CDbl(p.Value) + CDbl(d.Value)
    End Function
    the conversion to double will automatically assign 0 to empty cells, you can use cint or clng etc, what ever conversion is most appropriate for possible values
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2012
    Posts
    27

    Re: simple vba function

    thanks, will give this a try and mark resolved if it works...

Posting Permissions

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



Click Here to Expand Forum to Full Width