Easy (I think) VBA reference Question

sbeatton

Active Member
Joined
May 19, 2004
Messages
411
I want to use an if statement as below however I want it rather than look for 0 to look for blank cells, how do I reference it. Using "" does not seem to work, nor isempty(). Full code is

Sub aHideBlanks()
Dim c As Range
For Each c In Range("D12:D16").End(xlDown)
If c = 0 And c.Offset(, 3) = 0 Then c.EntireRow.Hidden = True
Next c
End Sub

Thanks,
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
hi!

Try if this works!
Code:
Sub aHideBlanks()
Dim c As Range
For Each c In Range("D12:D16")
If c = "" Then c.EntireRow.Hidden = True
Next c
End Sub
 
Upvote 0
Have tried "" previously but does not seem to want to work. Cells definitely have no value in them.

Any other ideas?
 
Upvote 0
your loop does not loop in all the cell in range.
have you tried the code?
it works for me when the cell is empty.
 
Upvote 0
Maybe I have made a mistake with my range. I am trying to check the array D12:G16 and if an entire row (eg. D12:G12) is blank hide that row. That is why I have said if c = D12:D16 AND the offset (3 columns to right) is blank then hide the row. Any suggestions.
 
Upvote 0
Problem was with the offset command. Needed to rewrite as follows

For Each c In Range("D12:D16")
If (c = "" And c.Offset(, 1) = "" And c.Offset(, 2) = "" And c.Offset(, 3) = "") Then c.EntireRow.Hidden = True
Next c

Wanted to check four columns and not just one. Is there any simpler way to write out of interest?
 
Upvote 0
hope this helps!

Code:
Sub hideCompRow()
Dim Myrange As Range
Dim Mycell As Range
Set Myrange = Range("D12:D16")
NumColToCeck = 4 'from column D to G
For Each Mycell In Myrange
    If Application.WorksheetFunction.CountA(Range(Mycell.Address & ":" & Mycell.Offset(0, NumColToCeck).Address)) = 0 Then Mycell.EntireRow.Hidden = True
Next Mycell

End Sub
 
Upvote 0

Forum statistics

Threads
1,203,628
Messages
6,056,414
Members
444,862
Latest member
more_resource23

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