Need help with If-Then-ElseIf in VBA

bronco

New Member
Joined
May 2, 2012
Messages
3
Pretty new to VBA and I'm trying to clean up the following code so that if the value of cell C964 = 0, then simply hide all the rows in the given range and stop there. Currently, it hides all of the rows and then takes another 30 seconds performing the second If function. I'm hoping you VBA vets can help me out! Thanks!

Sub HideUnusedRowsRS()
Dim cell As Range
For Each cell In Range("B4:B964")
If Range("C964") = 0 Then
Rows("4:964").Hidden = True
ElseIf cell.Value = "" Then
cell.EntireRow.Hidden = True
End If
Next
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Welcome to the Board!

The problem is that you're looping through each cell to evaluate it, which can take a long time, especially depending on what you're doing with it.

I'd take a look at using AutoFilter instead (which is recordable).

HTH,
 
Upvote 0
Thanks Smitty! Bummer... I was hoping to avoid using the autofilter, but that's probably the best option here.
Thanks for your quick advice!
 
Upvote 0
Glad I could help,

I was hoping to avoid using the autofilter, but that's probably the best option here.

Almost anytime you can do something natively as opposed to VBA (even calling upon intrinsic functionality from VBA) you'll be better off.
 
Upvote 0

Forum statistics

Threads
1,215,464
Messages
6,124,969
Members
449,200
Latest member
Jamil ahmed

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