VBA needed for hiding rows

LsuMustang

New Member
Joined
Jan 22, 2018
Messages
2
Good afternoon. On my password protected worksheet (NOx ppm), i am wanting to hide rows 38, 40, 52, and 57 based off the value in cell F40 on the same worksheet. The value in cell F40 is being pulled over from worksheet (Inputs). If the value in F40 is <=0, i want the rows 38, 40, 52, and 57 to hide (if >0, remain unhidden). Any help would be much appreciated. Thanks.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,

Try following simple code:

Code:
Sub Hide_Rows()
    
    If Sheets("Sheet1").Range("F40") <= 40 Then
    
    Range("A38,A40,A52,A57").EntireRow.Hidden = True
    
    Else
    
    Range("A38,A40,A52,A57").EntireRow.Hidden = False
    
    End If
    
End Sub

Cheers!!
 
Upvote 0
Thanks for the feedback, but i am not having any luck with it working. Which tab should i put the code in? Would password protection cause it to not work?

Hi,

Try following simple code:

Code:
Sub Hide_Rows()
    
    If Sheets("Sheet1").Range("F40") <= 40 Then
    
    Range("A38,A40,A52,A57").EntireRow.Hidden = True
    
    Else
    
    Range("A38,A40,A52,A57").EntireRow.Hidden = False
    
    End If
    
End Sub

Cheers!!
 
Upvote 0
Hi,

Replace ("Sheet1") with the name of worksheet where you want to hide rows.

If your worksheet is password protected then it will not work because owner of the worksheet has not allowed user to hide/unhide the rows.

If you are the owner of worksheet then we can add two more line code before (to unprotect the worksheet) and after of the code (to protect back the worksheet). Refer to the following code:

Code:
Sub Hide_Rows()
    
    Sheets("Sheet1").Unprotect Password:="123456" 'Key in your password to unprotect worksheet
    
    If Sheets("Sheet1").Range("F40") <= 40 Then
    
    Range("A38,A40,A52,A57").EntireRow.Hidden = True
    
    Else
    
    Range("A38,A40,A52,A57").EntireRow.Hidden = False
    
    End If
    
    Sheets("Sheet1").Protect Password:="123456" 'Key in your password to protect worksheet
    
End Sub

Replace password "123456" with your own password before running this code.

Hope it will help.

Cheers!!!
 
Last edited:
Upvote 0
Hi,

Open excel file, press ALT + F11. Insert MODULE. Copy and paste code in that module.

Replace "Sheet1" with the name of worksheet in which you want to hide rows.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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