Hiding Blank Rows in Certain Range thru Click Button

nhinx

Board Regular
Joined
Aug 25, 2016
Messages
55
Office Version
  1. 2010
Hi Everyone,

I got a VBA code from google and it works fine. However, I need to hide blank rows only in a specific range and the code below hide the entire range of rows. Thus anyone can amend the code below for my specific requirement? For example, I have a data from C4 to C10 and by doing so the blank rows from C11 to C15 will be hidden and by clicking again it will unhide.

Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Hide Blank Rows" Then
Range("C4:C15").EntireRow.Hidden = True
CommandButton1.Caption = "Show Blank Rows"
Else
Range("C4:C15").EntireRow.Hidden = False
CommandButton1.Caption = "Hide Blank Rows"
End If
End Sub


Many thanks,
Nhinx
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Do you mean:
Rich (BB code):
Private Sub CommandButton1_Click()
    
    With CommandButton1
        If .Caption = "Hide Blank Rows" Then
            Range("C11:C15").EntireRow.Hidden = True
            .Caption = "Show Blank Rows"
        Else
            Range("C11:C15").EntireRow.Hidden = False
            .Caption = "Hide Blank Rows"
        End If
    End With
    
End Sub
 
Upvote 0
Hi JackDanIce,

The Range should still be from C4:C15, if i will click the button which shows "Hide Blank Rows" the blank rows in Range C4:C15 should be hidden. When the button was switched to "Show Blank Rows"; then after clicking, all the blank rows from range C4:C15 that was hidden will be unhide.

Nhinx
 
Upvote 0
I've just tested your original code and it does hide and unhide with the caption updating for rows 4:15

Can you explain further what is not working?
 
Upvote 0
I have a data from Row C4 to C15 in which some of the cells has a value. For example I have data in C4, C6, C7, C10, C12, C14, C15. Then after clicking "Hide Blank Rows", the following rows C5, C8,C9, C11, C13, will be hidden. Then after it switched to "Show Blank Rows" by clicking the button again, all of the hidden rows will be shown again.

Thanks,

Nhinx
 
Upvote 0
Try:
Code:
Private Sub CommandButton1_Click()
    
    Dim rng     As Range
    Dim rngRow  As Range
    Dim rngHide As Range
    
    Set rngRow = Range("C4:C15")
    
    Application.ScreenUpdating = False
    
    With CommandButton1
        If .Caption = "Hide Blank Rows" Then
            For Each rng In rngRow
                If Len(rng.Value) = 0 Then
                    If rngHide Is Nothing Then
                        Set rngHide = rng
                    Else
                        Set rngHide = Union(rngHide, rng)
                    End If
            Next rng
            rngHide.EntireRow.Hidden = True
            .Caption = "Show Blank Rows"
            Set rngHide = Nothing
        Else
            rngRow.EntireRow.Hidden = False
            .Caption = "Hide Blank Rows"
        End If
    End With
    
    Application.ScreenUpdating = True
    
    Set rngRow = Nothing
    
End Sub
 
Upvote 0
Hi JackDanIce,

Thank you for the code. However, it has a Compile Error saying Next Without For which highlighting in yellow in Next Rng.

Thanks,
Nhinx
 
Upvote 0
Was missing an End If, try:
Code:
Private Sub CommandButton1_Click()
    
    Dim rng     As Range
    Dim rngRow  As Range
    Dim rngHide As Range
    
    Set rngRow = Range("C4:C15")
    
    Application.ScreenUpdating = False
    
    With CommandButton1
        If .Caption = "Hide Blank Rows" Then
            For Each rng In rngRow
                If Len(rng.Value) = 0 Then
                    If rngHide Is Nothing Then
                        Set rngHide = rng
                    Else
                        Set rngHide = Union(rngHide, rng)
                    End If
                End If
            Next rng
            rngHide.EntireRow.Hidden = True
            .Caption = "Show Blank Rows"
            Set rngHide = Nothing
        Else
            rngRow.EntireRow.Hidden = False
            .Caption = "Hide Blank Rows"
        End If
    End With
    
    Application.ScreenUpdating = True
    
    Set rngRow = Nothing
    
End Sub
 
Upvote 0
Hi JackDanIce,

This awesome. It works for my requirement. You're such an excel expert. Continue to share your knowledge.


Thank you very much,
Nhinx
 
Upvote 0
Hello Again JackDanIce,

I just have a litlle problem with the code. If all the rows from C4 to C15 has a data, it indicates Run-time error 91: Which says "Object variable or With block variable not set". If there are blank rows, there will be no problem.

Thanks,
Nhinx
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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