VBA WorksheetFunction- Set cell value to the the result of a formula

Posted on 2014-08-27
Last Modified: 2014-08-27

I have a large table in Excel 2013.
I need to filter columns 58-81, one at a time, for the value FALSE.
I then need to store the number of records found each time.
My table starts on Row 2, and I am putting the number of records found in row 1 of the column being filtered.
Table name is FSD.

I created this VBA to try to do this, but keep getting this error message:
Run-time error '424': Object Required.
I have not tried to add the loops yet, until I can get one pass to work.

Sub CountFiltered()

Dim TabFieldName As Variant
    ActiveSheet.ListObjects("FSD").Range.AutoFilter Field:=58, Criteria1:="FALSE"
    TabFieldName = "FSD[" & Cells(ActiveCell.Row + 1, ActiveCell.Column).Value & "]"
    ActiveCell.Value = Application.WorksheetFunction.Subtotal(3, TabFieldName)
End Sub

Open in new window

Question by:johnb25
    LVL 47

    Accepted Solution


    On line 89, TabField Name is a string

    Subtotal waits for a range like

    ActiveCell.Value = WorksheetFunction.Subtotal(3, Range("Table1[Column2]"))

    LVL 27

    Assisted Solution

    by:Glenn Ray
    You can't replace the header row with a numeric value in an Excel Table.  The ActiveCell is still in BF1 at line 9.

    Rather than use the subtotal function - since you want a static value displayed - why not just count the cells whose value is FALSE and insert that count in the first row at the end of the table?

    Sub Count_FALSE()
        Dim intCol As Integer
        Dim lngLR As Long
        Dim rngcheck As Range
        lngLR = Cells.SpecialCells(xlLastCell).Row + 2
        For intCol = 58 To 81
            Set rngcheck = Range(Cells(2, intCol).Address, Cells(lngLR - 1, intCol).Address)
            Cells(lngLR, intCol).Value = WorksheetFunction.CountIf(rngcheck, False)
        Next intCol
    End Sub

    Open in new window

    LVL 27

    Expert Comment

    by:Glenn Ray
    @Rgonzo1971...but if he redefines TabFieldName as a String variable, there will be a compile error at runtime on line 9 (type mismatch).  I tried that, too!

    I was mistaken about replacing the header row with numbers, but I forgot to mention that line 7 offsets down one row to try and determine the TabFieldName.  That won't work because the name would end up being either "FSD[TRUE]" or "FSD[FALSE]".
    LVL 6

    Author Comment

    Thanks both for your help...working code added below

    Using TabFieldName without quotes works as a Range.

    @Glenn_Ray Line 7 was offsetting from Row 1 (BF1), so the TabFieldName was correct.
    Also, I used the subtotal because I am filtering for FALSE on each column in turn, and only reporting the number of records found.
    Thanks for adding the Loop.

    Sub CountMdrFiltered()
    Dim TabFieldName As String
    Dim intCol As Integer
    For intCol = 58 To 81
        ActiveSheet.ListObjects("FSD").Range.AutoFilter Field:=intCol, Criteria1:="FALSE"
        TabFieldName = "FSD[" & Cells(2, intCol).Value & "]"
        Cells(1, intCol).Value = Application.WorksheetFunction.Subtotal(3, Range(TabFieldName))
    Next intCol
    End Sub

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I've recently been in need of an Excel macro that could add a letter before the text on multiple cells in an Excel document. My English is as it is, so I will try explain what it does diffrently. If you have an excel document with 2000 rows an…
    How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
    This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now