Hiding rows based upon a countif condition

macca_18380

New Member
Joined
May 15, 2024
Messages
22
Office Version
  1. 365
Hi, Again please forgive my lack of understanding, i'm learning but still a good way to go.

I would to automatically hide rows in a spreadsheet if input cells within a range are blank. In this case if all cells in the range J8 to AC8 are blank i'd like to hide rows 11 to 96, so i was thinking a countif function would be appropriate, but it doesn't work. Any help on correcting this code, or anything else that would work, would be very helpful, thank you!

If Not Intersect(Target, Range("J8:AC8")) Is Nothing Then

If CountIf(Range("J8:AC8"), "") = 20 Then
Rows("11:96").Hidden = True
End If

End If
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You can replace all this:
VBA Code:
If CountIf(Range("J8:AC8"), "") = 20 Then
Rows("11:96").Hidden = True
End If
with just this one line of code:
VBA Code:
If Application.WorksheetFunction.CountBlank(Range("J8:AC8")) = 20 Then Rows("11:96").Hidden = True
(you have to use "Application.WorksheetFunction" to use Excel functions in VBA).

However, I am concerned about this line of code:
VBA Code:
If Not Intersect(Target, Range("J8:AC8")) Is Nothing Then

If you are not updating a cell between J8 and AC8, this code will never run!
 
Upvote 0
You can replace all this:
VBA Code:
If CountIf(Range("J8:AC8"), "") = 20 Then
Rows("11:96").Hidden = True
End If
with just this one line of code:
VBA Code:
If Application.WorksheetFunction.CountBlank(Range("J8:AC8")) = 20 Then Rows("11:96").Hidden = True
(you have to use "Application.WorksheetFunction" to use Excel functions in VBA).

However, I am concerned about this line of code:
VBA Code:
If Not Intersect(Target, Range("J8:AC8")) Is Nothing Then

If you are not updating a cell between J8 and AC8, this code will never run!
Thank you Joe, the code you have supplied works perfectly and has allowed me to improve coding elsewhere also. Thank you for explaining the application.worksheetfunction also, that is very helpful. Thanks again!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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