Any idea how to shorten that VBA code for Hiding Rows

mazher

Active Member
Joined
Nov 26, 2003
Messages
359
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
VBA Code:
Range("A5,A6,A8,A9,A11,A12,A14,A15,A17,A18,A20,A21,A23,A24,A26,A27").EntireRow.Select
Selection.EntireRow.Hidden = True

1) Please can someone help me to shorten the above code.

2) Basically I need a a code to hide rows in the range (Row range 4:50 )

If there is a value in cell in Cell A4, I need to hide the rows (5:6), if there is a data in cell A7 in need to hide rows (8:9) and so on like that.

Thanks in advance
 
Thanks again @FryGirl and @dmt32

I do not know when I added your code in the module @dmt32, its not hiding the rows.

Might be I am doing some thing wrong.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Sorry my bad @dmt32

I was of the view that only by writing 1 in cell A1 will run the macro, but I have to run the macro manually.

Any chance it can be done like that, I have data in the Sheet2

Thanks again for all your help.
 
Upvote 0
Sorry my bad @dmt32

I was of the view that only by writing 1 in cell A1 will run the macro, but I have to run the macro manually.

Any chance it can be done like that, I have data in the Sheet2

Thanks again for all your help.

By Automatically does this mean A1 value is being changed by formula?

Dave
 
Upvote 0
By Automatically does this mean A1 value is being changed by formula?

Dave
Thanks for your reply.

I mean to say when i write 1 in cell A1 the code is executed and rows are hidden, when I delete 1 in cell A1 ( when its empty ) the rows are visible.

Hope this helps
 
Upvote 0
Thanks for your reply.

I mean to say when i write 1 in cell A1 the code is executed and rows are hidden, when I delete 1 in cell A1 ( when its empty ) the rows are visible.

Hope this helps

Hi,
delete earlier code & place ALL following code in your worksheets code page (Right Click Tab > View Code)

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
        If Target.Value = 1 Then Call HideUnhide(xlHidden)
        If Target.Value = "" Then Call HideUnhide(xlVisible)
    End If
End Sub

Sub HideUnhide(ByVal RowVisibility As Integer)
    Dim r As Long
    For r = 4 To 50 Step 3
            Cells(r, 1).Offset(1).Resize(2).EntireRow.Hidden = _
        IIf(RowVisibility = xlHidden, CBool(Cells(r, 1).Value > 0), False)
    Next r
End Sub

and see if this does what you want

Note: Solution is based on you making direct changes to Range A1 & not by formula.

Dave
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,749
Members
449,094
Latest member
dsharae57

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