why is this code hiding all rows in range?

orsm6

Active Member
Joined
Oct 3, 2012
Messages
496
Office Version
  1. 365
Platform
  1. Windows
hi - when i step through this code i found it hides all the rows in column a4:a50 that have nothing in them (they contain formulas, but cell is blank) but when i call the code as part of another macro it hides every row in a4:a50 regardless if it has values.

every row in that range has a formula, but if a4:50 is blank then the rest of the columns are blank too.

Code:
Sub hiderows()
Worksheets("Layout").Activate

Dim c As Range
Rows("4:50").EntireRow.Hidden = False
        'rehide the blanks
            For Each c In Range("a4:a50")
                If c.Value = "" Then
                    c.EntireRow.Hidden = True
                Else
                    'do nothing, can resinstate the command below, but not required
                    c.EntireRow.Hidden = False
            End If
            Next
End Sub
 
Good call still a bit strange how the other methods didnt work
i absolutely agree.... i picked my macro apart to see if there was another line conflicting, but couldn't nail it.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Still think it might be a corrupted sheet / module
 
Upvote 0
Still think it might be a corrupted sheet / module
Hi Michael,

It's not a corrupted sheet. :)

Based on all the comments, some info and explanations for you :

1. If you have one or more blanks in a cell, then the cell value has a LENGTH and then VBA code If <range> = "" will FAIL.

2. The reason in that situation that the column filter for excluding blanks still works in that situation is because Excel considers "" (empty) , " " (single blank), " " (multiple blanks) to be logically equivalent for filtering purposes. But VBA is more precise and considers one or more blanks is not equal to the empty string "".

3. Therefore, great that the filter solved your 2nd problem of "I want cells that look empty to be hidden" , but for your future benefit (and the benefit of others who might read this somewhat painful thread) you can eliminate such rows (or in your case make them PASS your if test, by changing your original code very slightly :

VBA Code:
If Trim(c.Value) = "" Then

The TRim() drops all the extra spaces so you end up with the empty string


4. HOWEVER, the more likely cause of the main problem ("Why is this hiding ALL the rows instead of just the empty ones ?" is that the cells that you think the code is walking between A4 and A50 are not on the Target Worksheet.

I had this issue in a recent macro where I defined a range something like you do ("A4:A50") and even though *I* though that the currently visible sheet was the Active Sheet (and thus the owner of the Range) Excel decided that the range was on some other sheet in another workbook that I also had open. Therefore to avoid this possible ambiguity, just make sure to explicitly identify the cells you are aiming to process, something like this :

VBA Code:
Sub hiderows()

Dim c As Range

    Worksheets("Layout").Rows("4:50").EntireRow.Hidden = False
        'rehide the blanks
            For Each c In Worksheets("Layout").Range("a4:a50")
                If Trim(c.Value) = "" Then
                    c.EntireRow.Hidden = True
                Else
                    'do nothing, can resinstate the command below, but not required
                    c.EntireRow.Hidden = False
                End If
            Next
End Sub

Another way to write this is :

VBA Code:
Sub hiderows()

Dim c As Range
With Worksheets("Layout")   [B]    [/B]

    .Rows("4:50").EntireRow.Hidden = False      '<<< NOTICE The "." added before "Rows" here
        'rehide the blanks
            For Each c In .Range("a4:a50")      '<<< NOTICE The "." added before "Range" here
                If Trim(c.Value) = "" Then
                    c.EntireRow.Hidden = True
                Else
                    'do nothing, can resinstate the command below, but not required
                    c.EntireRow.Hidden = False
                End If
            Next
End With
End Sub

What probably happened to you is that when you single-stepped the macro you started on the Layout sheet and kept flicking back to it to check whether lines were being hidden, which meant you were definitely processing the cells A4:A50 on the "Layout" sheet.

But when you run the macro from wherever it is run in the midst of your other Macros you might well be processing the empty cells A4 to A50 on some other sheet - that's why all the cells kept hiding.

This is also very possible if you had multiple workbooks open each with a "Layout" sheet.

5. Another way you could have checked that would have been to add a debug statement to your code like this :

VBA Code:
Sub hiderows()
Worksheets("Layout").Activate

Dim c As Range

    Rows("4:50").EntireRow.Hidden = False
        'rehide the blanks
            For Each c In Range("a4:a50")
                If Trim(c.Value) = "" Then
                    Debug.print "About to hide Row " & c.Row & " on Sheet " & c.WorkSheet.Name & " in workbook " &  c.Worksheet.Parent.Name
                    c.EntireRow.Hidden = True
                End If
            Next
End Sub

Then after your macro is run you check the output in the Immediate window (Ctrl-G) :

About to hide Row 3 on Sheet Layout in workbook Book43
About to hide Row 4 on Sheet Layout in workbook Book43
About to hide Row 5 on Sheet Layout in workbook Book43
About to hide Row 8 on Sheet Layout in workbook Book43
About to hide Row 9 on Sheet Layout in workbook Book43
...

Hope that helps clear things up for you (and makes you ultimately a better macro programmer ! :)

Cheers,

Warren.
 
Upvote 0
@tdh777
I don't know whether this post was actually for my benefit OR the original OP that asked the question, but if it was, I find it a little condescending, and at my age.....I don't think I want / need to be ""ultimately a better macro programmer ! """
But thanks for your comments, I look forward to the next lesson / lecture...(y)(y)
 
Upvote 0
Hi Michael,

It's not a corrupted sheet. :)

Based on all the comments, some info and explanations for you :

1. If you have one or more blanks in a cell, then the cell value has a LENGTH and then VBA code If <range> = "" will FAIL.

2. The reason in that situation that the column filter for excluding blanks still works in that situation is because Excel considers "" (empty) , " " (single blank), " " (multiple blanks) to be logically equivalent for filtering purposes. But VBA is more precise and considers one or more blanks is not equal to the empty string "".

3. Therefore, great that the filter solved your 2nd problem of "I want cells that look empty to be hidden" , but for your future benefit (and the benefit of others who might read this somewhat painful thread) you can eliminate such rows (or in your case make them PASS your if test, by changing your original code very slightly :

VBA Code:
If Trim(c.Value) = "" Then

The TRim() drops all the extra spaces so you end up with the empty string


4. HOWEVER, the more likely cause of the main problem ("Why is this hiding ALL the rows instead of just the empty ones ?" is that the cells that you think the code is walking between A4 and A50 are not on the Target Worksheet.

I had this issue in a recent macro where I defined a range something like you do ("A4:A50") and even though *I* though that the currently visible sheet was the Active Sheet (and thus the owner of the Range) Excel decided that the range was on some other sheet in another workbook that I also had open. Therefore to avoid this possible ambiguity, just make sure to explicitly identify the cells you are aiming to process, something like this :

VBA Code:
Sub hiderows()

Dim c As Range

    Worksheets("Layout").Rows("4:50").EntireRow.Hidden = False
        'rehide the blanks
            For Each c In Worksheets("Layout").Range("a4:a50")
                If Trim(c.Value) = "" Then
                    c.EntireRow.Hidden = True
                Else
                    'do nothing, can resinstate the command below, but not required
                    c.EntireRow.Hidden = False
                End If
            Next
End Sub

Another way to write this is :

VBA Code:
Sub hiderows()

Dim c As Range
With Worksheets("Layout")   [B]    [/B]

    .Rows("4:50").EntireRow.Hidden = False      '<<< NOTICE The "." added before "Rows" here
        'rehide the blanks
            For Each c In .Range("a4:a50")      '<<< NOTICE The "." added before "Range" here
                If Trim(c.Value) = "" Then
                    c.EntireRow.Hidden = True
                Else
                    'do nothing, can resinstate the command below, but not required
                    c.EntireRow.Hidden = False
                End If
            Next
End With
End Sub

What probably happened to you is that when you single-stepped the macro you started on the Layout sheet and kept flicking back to it to check whether lines were being hidden, which meant you were definitely processing the cells A4:A50 on the "Layout" sheet.

But when you run the macro from wherever it is run in the midst of your other Macros you might well be processing the empty cells A4 to A50 on some other sheet - that's why all the cells kept hiding.

This is also very possible if you had multiple workbooks open each with a "Layout" sheet.

5. Another way you could have checked that would have been to add a debug statement to your code like this :

VBA Code:
Sub hiderows()
Worksheets("Layout").Activate

Dim c As Range

    Rows("4:50").EntireRow.Hidden = False
        'rehide the blanks
            For Each c In Range("a4:a50")
                If Trim(c.Value) = "" Then
                    Debug.print "About to hide Row " & c.Row & " on Sheet " & c.WorkSheet.Name & " in workbook " &  c.Worksheet.Parent.Name
                    c.EntireRow.Hidden = True
                End If
            Next
End Sub

Then after your macro is run you check the output in the Immediate window (Ctrl-G) :

About to hide Row 3 on Sheet Layout in workbook Book43
About to hide Row 4 on Sheet Layout in workbook Book43
About to hide Row 5 on Sheet Layout in workbook Book43
About to hide Row 8 on Sheet Layout in workbook Book43
About to hide Row 9 on Sheet Layout in workbook Book43
...

Hope that helps clear things up for you (and makes you ultimately a better macro programmer ! :)

Cheers,

Warren.

a good explanation for my issue thanks.
i'm sure it was as painful for you to read as it was for the other guys trying to help me out and scratch their heads, i'm sure it would have been easier to share my workbook, but sensitive info is within. ... appreciate everyone helping and trying, it's no different trying to diagnose a car problem in a forum really. ...lots of guess work!
 
Upvote 0
@orsm6
I'm just glad you managed to get a solution...?
AND
yes, it is, and can be a very tedious process...but that's why we persist !
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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