+ Reply to Thread
Results 1 to 11 of 11

Using the Average formula in VBA gives different results to worksheet Average formula

  1. #1
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Using the Average formula in VBA gives different results to worksheet Average formula

    Hello,

    I'm hoping someone can help me with something peculiar that I can't seem to fix

    I'm needing to use the Average formula in VBA as the range that I need to average depends on the variables that are input by the user.

    Simply put, this is gi
    Please Login or Register  to view this content.
    Is giving a different result to it's worksheet equivalent:
    Please Login or Register  to view this content.
    VBA
    14, 5, 5 = 8.375
    Excel
    14, 5, 5 = 8

    I really would appreciate some help.

    Many thanks!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    When I do this the VBA code gives me 8. Exact same code.

    That range contains 5 cells. You gave 3 values. What values are in all 5 cells? Be specific--cell by cell. Specify any zeroes, specify blanks. Are there formulas in the cells, or values?

    It would be better to attach your file.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    Hi,

    Thanks for looking at this.

    That's interesting. Yes, there are formulas in the cells, with some cells showing "FALSE" as there is nothing to vlookup.

    Here are the cell values:

    Y6 - "11" ---> "=IF(COUNTA(U8),VLOOKUP(U8,Tables!$E$2:$F$17,2,0))"
    Y7 - "5" ---> "=IF(COUNTA(U7),VLOOKUP(U7,Tables!$E$2:$F$17,2,0))"
    Y8 - "FALSE" ---> "=IF(COUNTA(U8),VLOOKUP(U8,Tables!$E$2:$F$17,2,0))"
    Y9 - "FALSE" ---> "=IF(COUNTA(U9),VLOOKUP(U9,Tables!$E$2:$F$17,2,0))"
    Y11 - "5" ---> "=IF(COUNTA(U11),VLOOKUP(U11,Tables!$E$2:$F$17,2,0))"

    I just tried to run the function with the value 5 in place of the "FALSE" value. So:
    Y6 - "11" ---> "=IF(COUNTA(U8),VLOOKUP(U8,Tables!$E$2:$F$17,2,0))"
    Y7 - "5" ---> "=IF(COUNTA(U7),VLOOKUP(U7,Tables!$E$2:$F$17,2,0))"
    Y8 - "5" ---> "=IF(COUNTA(U8),VLOOKUP(U8,Tables!$E$2:$F$17,2,0))"
    Y9 - "5" ---> "=IF(COUNTA(U9),VLOOKUP(U9,Tables!$E$2:$F$17,2,0))"
    Y11 - "5" ---> "=IF(COUNTA(U11),VLOOKUP(U11,Tables!$E$2:$F$17,2,0))"

    This resulted in:

    VBA
    = 6.875
    Excel
    = 6.8

    I should also note that when I take away the second range:
    Please Login or Register  to view this content.
    It works fine, regardless of the "FALSE" values.

    See the link to the uploaded file below.
    Last edited by Sc0ut; 03-05-2016 at 11:38 AM.

  4. #4
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    I've just tried it with static values (i.e. I replace the formula with the actual value e.g. "5") and I get the same problem.

    I have reproduced the problem. The attachment button isn't allowing me to attach the file so I've linked the file here.

    Note: When you delete the formula in A5, the problem goes away. Very unusual.

    It seems that the VBA isn't respecting the break between the first and second range. Instead, it is just including that in it's calculation.
    Last edited by Sc0ut; 03-05-2016 at 11:36 AM.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    Change into:
    Please Login or Register  to view this content.
    PS as for attachments - you may also use Manage Attachments below text edit window in Advanced view, not only paperclip above text edit window
    Best Regards,

    Kaper

  6. #6
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    Thank you so much, Kaper!

    And thanks for the advice about the attachments. Champion.

  7. #7
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    I need to use this approach with variables (otherwise I wouldn't be using the formula in VBA) and I'm having problems getting it to work. Would anyone be able to help with the syntax?

    Please Login or Register  to view this content.
    This executes in the same way that this did:

    Please Login or Register  to view this content.
    That is to say that it sees the two ranges as continuous, from Y6:Y11.

    As Kaper pointed out, this does work:

    Please Login or Register  to view this content.
    What would this look like when using the variables that I've included above?
    Last edited by Sc0ut; 03-05-2016 at 08:36 PM.

  8. #8
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    I've tried this to no avail:

    Please Login or Register  to view this content.
    This has the same problem with seeing the range as continuous.

  9. #9
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    Surely there is a way to do this with variables :/

    This formula needs to be used in loops for many cells and rows.

  10. #10
    Registered User
    Join Date
    08-04-2013
    Location
    Brisbane
    MS-Off Ver
    Excel for Mac 2021
    Posts
    97

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    I got it!

    Please Login or Register  to view this content.
    I hope that helps someone!

    Thanks for the help.
    Last edited by Sc0ut; 03-06-2016 at 02:01 AM.

  11. #11
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,613

    Re: Using the Average formula in VBA gives different results to worksheet Average formula

    Glad to hear that you managed to solve it. And thanks for the reputation

+ 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. [SOLVED] Average formula that shows 0 but does not count in average
    By Lewster in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 11-11-2015, 04:49 PM
  2. Average Results from a Formula
    By nfteaudrey in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-10-2015, 04:28 PM
  3. [SOLVED] Rank numbers by average with doing average formula
    By makinmomb in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 08-26-2014, 11:58 PM
  4. Trying to create a formula to average survey results per instructor
    By nscarritt in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-28-2014, 08:55 PM
  5. [SOLVED] need formula to average cells but if one cell has N/A then dont average and input N/A
    By CityInspector in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-08-2013, 04:16 PM
  6. [SOLVED] Using cell value as worksheet name in formula reference(AVERAGE LARGE IF)
    By trizzo in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-26-2013, 09:23 PM
  7. Replies: 0
    Last Post: 01-22-2013, 12:22 PM

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