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
 
Adjusted, 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
            On Error Resume Next
            rngHide.EntireRow.Hidden = True
            On Error GoTo 0
            .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

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi JackDanICe,

This is fantastic. That solved my problem. A million thanks to you.

Regards,
Nhinx
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

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