How are you hiding those cells, if it's via filtering or by hiding the row you can use SUBTOTAL function like this

=SUBTOTAL(109,A1:A100)

....or you can also try AGGREGATE function

=AGGREGATE(9,5,A1:A100)