+ Reply to Thread
Results 1 to 2 of 2

Average Days in filter - based on value in same row plus VBA

  1. #1
    Registered User
    Join Date
    03-05-2009
    Location
    Vail, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    83

    Smile Average Days in filter - based on value in same row plus VBA

    Forum example:
    {=AVERAGE(IF((SUBTOTAL(3,OFFSET(raw!$U$2:$U$64999,ROW(raw!$U$2:$U$64999)-MIN(ROW(raw!$U$2:$U$64999)),0,1)))*(ISNUMBER(MATCH(raw!$U$2:$U$64999,{2,3,4,5},0))),raw!$AB$2:$AR$64 999))}

    Control+shift+enter
    =average(if((subtotal(3,OFFSET((W6:W34, ROW(W6:W34-MIN(ROW(W6:W34)),0,1)))*(MATCH(("Original", R6:R34)))

    Column R - Original, Extension, <blank>
    Column W - Days (numeric)
    Row 6 to Row 34

    When filter is used, show average number of days with "Original"

    Next, I need to program this into a VBA code in Access.
    I will substitute the RowCount inside the formula (once I get it working)

  2. #2
    Registered User
    Join Date
    03-05-2009
    Location
    Vail, Colorado
    MS-Off Ver
    Excel 2010
    Posts
    83

    Re: Average Days in filter - based on value in same row plus VBA

    Figured it out, now just have a pesky divide by zero error if no data is in the filtered selection:
    Since this is entered with Control+Shift+Enter how do we wrapt this into an error handler?

    The following code gets a div/0 error if there is no data showing in the filtered column. A error condition needs to wrap this Array Formula to prevent this.
    Did this a year ago and can't find my notes. Anyone have any suggestions?

    After copying a recordset over to Excel (vba)
    Column W has Days (numeric)
    Column R has type (text) Only the matching rows with "Original" will be averaged - and only those rows visible from the Autofilter.

    Problem - the divide by zero.


    Code:
    ' where ObjXL is a reference to the Excel Workbook created by automation
    ObjXL.Range("A2").Select
    Selection.FormulaArray = _
    "= AVERAGE(IF((SUBTOTAL(3,OFFSET(R[4]C[22]:R[32]C[22], ROW(R[4]C[22]:R[32]C[22])-MIN(ROW(R[4]C[22]:R[32]C[22])),0,1)))*(R[4]C[17]:R[32]C[17]= ""Original""),R[4]C[22]:R[32]C[22]))"
    ObjXL.Range("A2").Select
    ObjXL.Selection.NumberFormat = "0" ' no decimals
    ' Note to manually enter this array formula in Excel, remember to use Control+Shift+EnterExcel Formula Bar:
    = AVERAGE(IF((SUBTOTAL(3,OFFSET(W6:W4656, ROW(W6:W4656)-MIN(ROW(W6:W4656)),0,1)))*(R6:R4656= "Original"),W6:W4656))

    Control+Shift+Enter of the formula above creates the array formula in Excel shown in the Code Window.
    My vba code substitutes the "4656" for the actual Row Count number obtained from the CopyFromRecordset process.
    So, each formula matches the start position (row 6) and covers the last row inserted into Excel

+ 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.6.0 RC 1