+ Reply to Thread
Results 1 to 10 of 10

Something wrong with the IF statement

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Something wrong with the IF statement

    Hi everyone,

    I have a simple macro below for my purpose. This function always returns 0 if there are error values in the range (e.g. #N/A, #DIV/0!). I feel there is something wrong with the IF statement?


    Function CustomAVG(rData As Range, dPct1 As Double, dPct2 As Double, Optional dLower As Double, Optional dUpper As Double) As Double
        Dim i As Long, c As Range, a() As Variant
        i = -1
        On Error Resume Next
        For Each c In rData
            If Not IsError(c.Value + 0) And (Len(c.Value) > 0) Then
                i = i + 1
                ReDim Preserve a(i) As Variant
                a(i) = c
            End If
        Next c
        CustomAVG = WorksheetFunction.Percentile(a, dPct1)
    End Function

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Something wrong with the IF statement

    I feel there is something wrong with the IF statement?
    Care to explain?

  3. #3
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Something wrong with the IF statement

    Not sure, but I think the problem is in the values you move into the array.
    Try changing that to
    a(i) = CDbl(c)
    Then the function gives a value other than 0.
    Not sure if that's the value you're looking for.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  4. #4
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Something wrong with the IF statement

    HI Tsjallie, it does work! May I ask why do I need to force the conversion? I thought array a() is defined as type variant which can accept any values.

  5. #5
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Something wrong with the IF statement

    As you say the array is of type variant, so will accept any kind of data.
    Problem is with the percentile funtion which needs numeric data. The array gets fed with things like "Error 2007" or alike.
    With cdbl (or may be that should be clng) the errorcode is isolated and so gets fed with numeric data.

  6. #6
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Something wrong with the IF statement

    Hi Tsjallie, can you look at the code below? There is no use of Cdbl but still worked Thank you for helping.

    Function fPercentile(rData As Range, dPercentile As Double) As Double
        Dim i As Long, c As Range, a() As Variant
        i = -1
        For Each c In rData
          If Not IsError(c) Then
            i = i + 1
            ReDim Preserve a(i) As Variant
            a(i) = c
          End If
        Next c
        fPercentile = WorksheetFunction.Percentile(a, dPercentile)
    End Function

  7. #7
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Something wrong with the IF statement

    Took a closer look at the macro and found that I need glasses. Thought you wanted to assemble error codes in the array (and wndeered why), but you're not. Overlooked the NOT if the if-statement.
    Problem is in the len-function.
    If a cell with an error is evaluated then the len-function gives a type mismatch.
    But, since you have set On Error Resume Next the function happily proceeds with the next line.
    So these non-numeric data get into the array.

    What you need to do is split up the if-statement into two parts. And I also think you can leave out the On Error statement.
    Function CustomAVG(rData As Range, dPct1 As Double, dPct2 As Double, Optional dLower As Double, Optional dUpper As Double) As Double
        Dim i As Long, c As Range, a() As Variant
        i = -1
        'On Error Resume Next
        For Each c In rData
            If Not IsError(c.Value) Then
                If Len(c.Value) > 0 Then 'The len function is now only executed for cells not having an error
                    i = i + 1
                    ReDim Preserve a(i) As Variant
                    a(i) = c
                End If
            End If
        Next c
        CustomAVG = WorksheetFunction.Percentile(a, dPct1)
    End Function
    This also explains why the other function did work.

  8. #8
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Something wrong with the IF statement

    Hi Tsjallie, thank you so much for the great explanation. The new code you provided now works perfectly. Very good knowledge to me. Thank you.

    May I ask the final question: if I change the statement:

    If Not IsError(c.Value) Then
    to
    If Not IsError(c.Value + 0) Then
    The function resulted in #VALUE!. If I remove the part + 0, then the function works. Do you know why?

  9. #9
    Forum Contributor
    Join Date
    01-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003, 2007, 2010, 2019
    Posts
    436

    Re: Something wrong with the IF statement

    The following code will result in #VALUE! error. But it will work if the c.Value + 0 becomes c.Value. Not sure why? The sample file is attached.

    Function CustomPct(rData As Range, dPct As Double) As Double
        Dim i As Long, c As Range, a() As Variant
        i = -1
        For Each c In rData
            If Not IsError(c.Value + 0) Then
                If Len(c.Value) > 0 Then
                    i = i + 1
                    ReDim Preserve a(i) As Variant
                    a(i) = c
                End If
            End If
        Next c
        CustomPct = WorksheetFunction.Percentile(a, dPct)
    End Function
    Attached Files Attached Files

  10. #10
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: Something wrong with the IF statement

    That's (probably) because when c contains an error doing an operation on it (c.value +0) gives a type mismatch an so the result cannot be calculated

+ 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. What's wrong with this IF statement?
    By djarcadian in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-14-2013, 04:33 PM
  2. What's wrong with this =if and =and and =or statement
    By soysoy05 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-05-2011, 02:57 AM
  3. wrong:IF statement
    By scubadiver007 in forum Excel General
    Replies: 2
    Last Post: 03-28-2011, 11:47 AM
  4. If Statement, or am I going about this wrong?
    By JJAndradeIII in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-11-2008, 06:49 AM
  5. What is wrong with this If statement?
    By shaolin in forum Excel General
    Replies: 3
    Last Post: 01-30-2008, 01:11 PM
  6. [SOLVED] IF Statement help please - again - got it wrong last time!
    By Alan Davies in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-12-2006, 07:30 AM
  7. [SOLVED] wts wrong with this statement
    By Jason in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-24-2006, 10:30 AM
  8. [SOLVED] What is wrong with this IF statement? need help.
    By AC man in forum Excel General
    Replies: 4
    Last Post: 01-11-2006, 11:35 AM

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