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

Hi,

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
    
    Range("BF1").Select
    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

LVL 6
johnb25Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

On line 89, TabField Name is a string

Subtotal waits for a range like

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

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Glenn RayExcel VBA DeveloperCommented:
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


Regards,
-Glenn
0
Glenn RayExcel VBA DeveloperCommented:
@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]".
0
johnb25Author Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.