(VBA) Count Filled Cells In Column After Filtering, And Store It as a variable?

evxret

New Member
Joined
Apr 8, 2022
Messages
38
Office Version
  1. 365
Platform
  1. Windows
I have a basic set of data with filters applied, I am simply trying to Figure out how to store a Numeric variable that is the count of filled cells.
Example: I have 100 rows before applying filters, I filter by date and now data is only 10 rows, I tried using End(xlUp) and End (xlDown).row, but this is returning the value of the bottom row in the data, and because it's filtered, its not the same amount of rows filled. (i.e. returning 13 as a variable because thats the last row data is on, but theres only 10 open rows.)

I've also attempted this but It's saying object required so I can't figure out how this function would return a number.

VBA Code:
Sub Count_cells_fromRange()
Dim ws As Worksheet
Dim RowNum As Long

Set ws = Worksheets("Test Ship Report")
Set RowNum = Application.WorksheetFunction.CountA(ws.Range("B:B"))

MsgBox ("Count Of Cells Is " & RowNum)
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Perhaps this?

VBA Code:
Sub Count_cells_fromRange()
    Dim ws As Worksheet
    Set ws = Worksheets("Test Ship Report")
    
    MsgBox "Count Of Cells Is " & _
    WorksheetFunction.Subtotal(3, ws.Range("B2", ws.Cells(Rows.Count, "B").End(xlUp)))

End Sub
 
Upvote 0
You only use Set when dealing with objects, not numbers, so just remove the Set for the RowNum line.
 
Upvote 0
Solution
You only use Set when dealing with objects, not numbers, so just remove the Set for the RowNum line.
I truly appreciate all your help. This worked. Its always something super simple that gets overlooked. Sorry I'm new
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top